Hdu-3336-Count the string-【KMP】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3336


Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10897    Accepted Submission(s): 5073


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
 

Recommend
lcy
 

题意:计算所有S的前缀在S中出现了几次

解题:好坑的题,想半天。有好几种方法,看大神们用dp  惊讶住

1>dp  状态转移方程:dp[i]=dp[next[i]]+1;

        解释这个方程,举个例子  s 为 ababab

         i :   1 2 3 4 5 6
     字符串:    a b a b a b   
      dp[i]:    1 1 2 2 3 3

  当i为5时   对应字符串 ababa  后缀aba和前缀aba重叠

 以第三个a为结尾的前缀总数对应于前面的aba的前缀数+1(ababa)

即dp[5]=dp[3]+1(加1为整体算是一个)

代码

#include<cstdio>
#include<cstring>
char s[200005];
int dp[200005];
int next[200005],n;
void Next()
{
	int i=0,j=-1;
	next[0]=-1;
	while(i<n)
	{
		if(s[i]==s[j]||j==-1)
			next[++i]=++j;
		else
			j=next[j];
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		scanf("%s",s);
		int sum=0;
		Next();
		for(int i=1;i<=n;i++)
		{
			dp[i]=dp[next[i]]+1;//dp[i] 表示 以i结尾的子串与前缀相等的个数 d[i]=d[next[i]]+1;
			sum+=dp[i];
			sum%=10007;
		}
		printf("%d\n",sum);
	}
	return 0;
}
/*1
4
abab
*/ 

另外一个方法

这个http://blog.csdn.net/creat2012/article/details/21741013  这个帖子讲得很好

题意:求字串中【前缀+跟前缀相同的子串】的个数?

Sample Input
1
4
abab

Sample Output
6

abab:包括2个a,2个ab,1个aba,1个abab


这里要用到next值的意义:
next[i]表示前i个字符所组成的字符串最大前后缀匹配长度
举个例子:

next[5]=2, 表示下标5前面那个字符串abcab的前后缀匹配的最大长度是2,显然就是ab了

回到本题:
所求=字串的前缀个数+与前缀相同的子串
问题可以部分转化为:每个前缀的最大前后缀匹配问题

继续用上面的例子:

第一步:

对于这段子串,next[5]=2,然后后面不符合next值的递增规律了。
所以对于abcab,长度为2的后缀,即ab与前缀匹配
所以+1个ab,注意还要+1个a,既然后缀ab跟前缀ab匹配,则必有a跟前缀匹配。
也就是+2个了,其实实际上+next[5]就可以了,因为这是最长前后缀匹配长度

第二步:

对于这段子串:
next[6]=1,然后后面不符合next值的递增规律了。
所以对于abcaba,长度为1的后缀,即a与前缀匹配。
所以+1个a,也就是+next[6]了。

第三步:

对于整个串:
next[12]=4后面没有了
所以对于整个串:abcabacbabca,长度为4的后缀跟前缀匹配
所以+1个abca,+1个abc,+1个ab,+1个a,总共+4个,也就是+next[12]了

最后:
好了,刚刚一共+了7个与前缀匹配的子串
上面说了题目是求:字串的前缀个数+与前缀相同的子串个数
与前缀相同的子串个数就是7个了
然后字串的前缀个数当然就是整个串的长度了,那么就是12个

加起来就是答案:19。

到这里,有些同学就要问了,为什么next[4]为什么就不做处理了呢??

我一开始也是这么想的,为什么呢??

因为,next[4]的后面的next[5]==(正好)next[4]+1。这不是偶然,因为abca的最大后缀为1(a),而abcab的最大后缀为2(ab),这里的ab包含了前面的a。所以,如果正好next[i]+1=next[i]的话,前面的被后面包含了。

代码:

[cpp]  view plain  copy
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <algorithm>  
  4. #include <cmath>  
  5. using namespace std;  
  6.   
  7. const int N=200003;  
  8. char Str[N];  
  9. int n,next[N];  
  10.   
  11. void Get_Next(){  
  12.     next[0]=-1;  
  13.     int j=-1,i=0;  
  14.     while(i<n){  
  15.         if(j==-1||Str[i]==Str[j]){  
  16.             j++;i++;  
  17.             next[i]=j;  
  18.         }  
  19.         else j=next[j];  
  20.     }  
  21. }  
  22.   
  23. int main(){  
  24.     int T;  
  25.     scanf("%d",&T);  
  26.     while(T--){  
  27.         scanf("%d",&n);  
  28.         scanf("%s",Str);  
  29.         Get_Next();  
  30.         int ans=n+next[n];  
  31.         for(int i=0;i<n;i++){  
  32.             if(next[i]>0&&next[i]+1!=next[i+1])  
  33.             ans=(ans+next[i])%10007;  
  34.         }  
  35.         printf("%d\n",ans);  
  36.     }  
  37.     return 0;  
  38. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值