Count the string

Count the string

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 13
Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: "abab" The prefixes are: "a", "ab", "aba", "abab" For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6. The answer may be very large, so output the answer mod 10007.
 

 

Input
The first line is a single integer T, indicating the number of test cases. For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 

 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

 

Sample Input
1 4
abab
 

 

Sample Output
6
 

 

Author
foreverlin@HNU
 

 

Source
HDOJ Monthly Contest – 2010.03.06
 
t这一题目纯粹的KMP字符匹配,第一行输入T组测试样例。然后输入N,表示要输入一个长度为N的字符串。对于该字符串,把该字符串的一次分
解成1,  2,3...N长度的子串,然后拿去和主串匹配。把每一次能够匹配的次数累加起来,就是所要求的的答案、
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 char S[2000050],t[200005];
 5 int Len_S,Len_t,num[200005];
 6 void getNext(char p[],int next[])
 7 {
 8     int j,k,L;
 9     next[0]=-1;
10     j=0;
11     k=-1;
12     while(j<Len_t-1)
13     {
14         if(k==-1||p[j]==p[k])
15         {
16             j++;
17             k++;
18             next[j]=k;
19         }
20         else
21             k=next[k];
22     }
23 }
24 
25 int KMPMatch(char s[],char p[])
26 {
27     int next[200005];
28     int i,j,sign=0;
29     i=0;
30     j=0;
31     getNext(p,next);
32     while(i<Len_S)
33     {
34         if(j==-1||s[i]==p[j])
35         {
36             i++;
37             j++;
38         }
39         else
40             j=next[j];
41         if(j==Len_t)
42             {sign++;j=next[j-1];i=i-1;}
43     }
44     return sign;
45 }
46 int main()
47 {
48     int T,i,Sign,j,sum;
49     scanf("%d",&T);
50     while(T--)
51     {
52         scanf("%d",&Len_S);
53         getchar();
54         gets(S);
55         for(i=0,sum=0;i<Len_S;i++)
56         {
57             for(j=0;j<=i;j++)/*获取每一次的子串*/
58                 t[j]=S[j];
59             t[j]='\0';
60             Len_t=j;
61             num[i]=KMPMatch(S,t);/*进行KMP字符匹配,返回匹配的次数*/
62             sum+=num[i];
63             if(num[i]==1)
64             {
65                 sum+=(Len_S-Len_t);
66                 break;
67             }
68         }
69 
70        /* for(i=0;i<Len_S;i++)
71             printf("%d\n",num[i]);*/
72         printf("%d\n",sum%10007);
73     }
74     return 0;
75 }
View Code

 模板更新(2016.4.20):

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 int Len;
 7 void getNext(char p[],int next[],int Len_t)
 8 {
 9     int i=0,j=-1;
10     next[0]=-1;
11     while(i<Len_t){
12         if(j==-1||p[i]==p[j])next[++i]=++j;
13         else j=next[j];
14     }
15 }
16 int KMPMatch(char s[],char p[],int ttt)
17 {
18     int next[200005];
19     int i=0,j=0,sign=0;
20     int Len_t=ttt,Len_S=strlen(s);
21     getNext(p,next,Len_t);
22     while(i<Len_S){
23         if(j==-1||s[i]==p[j]){i++;j++;}
24         else j=next[j];
25         if(j==Len_t){sign++;j=next[j-1];i=i-1;}
26     }
27     return sign;     /*返回值是子串在主串重复的次数*/
28 }
29 char Str[2000050];
30 char sss[2000050];
31 int main()
32 {
33     int T,i,Sum,nnn;
34     scanf("%d",&T);
35     while(T--){
36         scanf("%d %s",&Len,Str);
37         Sum=0;
38         for(i=0;i<=Len;i++){sss[i]=Str[i];}
39         for(i=1;i<Len;i++){
40             nnn=KMPMatch(Str,sss,i);
41             Sum+=nnn;
42             if(nnn==1){
43                 Sum+=Len-i-1;break;
44             }
45         }
46         printf("%d\n",(Sum+1)%10007);
47     }
48     return 0;
49 }
View Code

 

 

转载于:https://www.cnblogs.com/Wurq/articles/3888628.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值