HDU 3336 Count the string(dp+kmp)

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

应该说是一道常规dp+计数转移,最后自己推出来关系式,然而明明写的都和网上搜的答案高度一致了。
自己的程序就是WA,网上的就能AC,最玄学的题...
思路:就是如果能从i转移到next[i],就会在答案中继承next[i]对应的ans部分;
而最长前缀与最长后缀相等,贡献了额外的一个答案。
即核心部分:if(j!=-1)a[i]==a[j]?ans[i]=ans[j]+1:0;

自己的代码(WA):

//将s1串循环接到自己身上,一遍即可 
/*abcdabc next[6]=3时 a=a b=b c=c ab=ab bc=bc abc=abc 6 即ans+=3+2+1  
aaaabaaaa
next[0]=-1 ans[0]=0
next[1]=0 ans[1]=0
next[2]=1 ans[2]=1
next[3]=2 ans[3]=1,ans[3]+=ans[2]=2
next[4]=3 ans[4]=1,ans[4]+=ans[3]=3
next[5]=0 ans[5]=0
123=234 ans[4]=1
12=23 ans[3]=1

能推出23=34即12=34 是一组新解
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mod=10007;
char t[200005];
int net[200005],ans[200005];
int kmppre(char s[],int m)
{
	int i,j=net[0]=-1,res=0;
	while(i<m)
	{
		if(s[i]==s[j])
		{
		ans[i]=ans[j]+1;
		res+=ans[i];
		if(res>=mod)res%=mod;
	    net[++i]=++j; 
	    }
	    else if(j==-1)net[++i]=++j;
	    else j=net[j];
	}
	return (res+m)%mod;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(ans,0,sizeof(ans));
	    int n;
	    scanf("%d%s",&n,t);
		printf("%d\n",kmppre(t,n));
    }
	return 0;
} 

代码来自:

https://blog.csdn.net/u011686226/article/details/22044935

https://www.cnblogs.com/yym2013/p/3550775.html

AC代码①:

#include <iostream>
#include <string.h>
using namespace std;
char s[200001];
int nnext[200001];
int c[200001];
int ans;
void GetNext(char t[],int nnext[])
 {
    memset(c,0,sizeof(c));
   int j,k;
     j=0;k=-1;nnext[0] = -1;
     int length;
     for(length = 0;t[length]!='\0';length++);
     while(j<length){
         if(k==-1 || t[j]==t[k]){
             if(k!=-1){
                 c[j] = c[k] + 1;
                 ans+=c[j];    
             }
             j++;k++;
             nnext[j] = k;
         }
         else
             k = nnext[k];
     }
 }
 int main()
 {
     int T,n;
     cin>>T;
     while(T--){         cin>>n;
         cin>>s;
         ans = 0;
         GetNext(s,nnext);
         cout<<(ans+n)%10007<<endl;
     }
     return 0;
 }

AC代码②:

//注意f[1]=0,dp[0]=0的操作
//分f和dp两步进行操作,操作较为常规

#include <cstdio>
#include <cstring>
const int maxn = 200010;
const int mod = 10007;
char a[maxn];
int f[maxn];
int dp[maxn];
int n, m;
 
void getFail()
{
	f[0] = f[1] = 0;
	for(int i = 1; i < n; i++)
	{
		int j = f[i];
		while(j && a[i] != a[j])
			j = f[j];
		f[i+1] = a[i] == a[j] ? j+1 : 0;
	}
	dp[0] = 0;
	for(int i = 1; i <= n; i++)
	{
		dp[i] = dp[f[i]]+1;
		m += dp[i];
		m %= mod;
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		m = 0;
		scanf("%d %s", &n, a);
		getFail();
		printf("%d\n", m);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Code92007/article/details/81073658