hdu3336 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.youkuaiyun.com/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;
}

 

资源下载链接为: https://pan.quark.cn/s/d0b0340d5318 Cartopy安装所需包分为两个部分,分别需要下载。以下是下载链接和建议的操作步骤: Cartopy安装所需包2:Cartopy安装所需包2.rar 安装教程:Cartopy安装教程之pip篇 下载文件: 首先,分别下载上述两个链接中的文件。第一个链接包含了Cartopy安装所需的包(部分),第二个链接是详细的安装教程。 建议将下载的文件解压后,统一放在一个路径下,例如命名为“Cartopy安装文件”的文件夹,方便后续操作。 参考安装教程: 安装教程详细介绍了通过pip安装Cartopy的步骤,包括环境变量设置、下载必要安装包、安装过程以及测试。 根据教程,需要安装的依赖包包括numpy、pyshp、Shapely、pyproj、Pillow等,教程中还提供了针对Windows系统的预编译版本下载链接。 安装过程中可能会遇到缺少pykdtree和scipy模块的情况,教程也提供了相应的解决方法。 安装注意事项: 确保Python环境变量已正确设置,可通过命令行输入python --version来验证。 安装Wheel工具,用于安装.whl文件。 按照教程中的命令依次安装各个依赖包,注意版本号需与Python版本匹配。 如果遇到缺少模块的错误,按照教程中的方法进行安装。 通过以上步骤,可以顺利完成Cartopy的安装。如果在安装过程中遇到问题,可以参考安装教程中的详细说明或在相关社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值