题意很简单,给一个数n 以及一个字符串str,区间【i,i+n-1】 为一个新的字符串,i 属于【0,strlen(str)】如果新的字符串出现过ans++,例如:acmacm n=3,那么 子串为acm cma mac acm ,只有acm出现过
求ans;
2 2 aaaaaaa 3 acmacm
5 1
第一次看到10W以为会超时呢 然而并没有....
就是分割字符串,然后把分割的字符串存贮到结构体中(有点丢人了。只会用结构体的字符串排序,qsort的忘记了...)
然后对结构体的字符串进行排序,如果有两个字符串相同ans++;
代码:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define num 100005 char str[num],temp[11]; struct node { char dir[11]; }c[num]; bool cmp(node x,node y)//对结构体的字符串排序 { if(strcmp(x.dir,y.dir)<0) return true; return false; } int main() { int test,n,i,t,len; scanf("%d",&test); while(test--) { scanf("%d %s",&n,str); len=strlen(str); i=0; while(i+n<=len)//分割字符串并存贮字符串 { t=0; for(int q=i;q<i+n;q++) temp[t++]=str[q]; temp[t]='\0'; strcpy(c[i].dir,temp); memset(temp,0,sizeof(temp)); i++; } sort(c,c+i,cmp); int ans=0; for(int j=1;j<i;j++) if(strcmp(c[j].dir,c[j-1].dir)==0) ans++; printf("%d\n",ans); } return 0; }
原文地址:http://blog.csdn.net/su20145104009/article/details/45873299