HDU 3336 Count the string(kmp)
2016-08-31 22:09
351 查看
本题主要是考察kmp算法中next数组的求法及含义:
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int nxt[200005];
int n;
char s[200005];
void get_next()
{
int i,j;
i=0;
j=-1;
nxt[0]=-1;
while(i<n)
{
if(j==-1||s[i]==s[j])
{
nxt[++i]=++j;
}
else
{
j=nxt[j];
}
}
}
int main()
{
int T,sum,j;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%d",&n);
scanf("%s",&s);
get_next();
for(int i=1; i<=n; i++)
{
j=i;
while(j)
{
sum=(sum+1)%10007;
j=nxt[j];
}
}
printf("%d\n",sum);
}
}
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int nxt[200005];
int n;
char s[200005];
void get_next()
{
int i,j;
i=0;
j=-1;
nxt[0]=-1;
while(i<n)
{
if(j==-1||s[i]==s[j])
{
nxt[++i]=++j;
}
else
{
j=nxt[j];
}
}
}
int main()
{
int T,sum,j;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%d",&n);
scanf("%s",&s);
get_next();
for(int i=1; i<=n; i++)
{
j=i;
while(j)
{
sum=(sum+1)%10007;
j=nxt[j];
}
}
printf("%d\n",sum);
}
}
相关文章推荐
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string(KMP)
- HDU 3336 Count the string KMP
- [HDU 3336]Count the String[kmp]
- HDU--杭电--3336--Count the string--KMP--说是KMP,实际我是用的滑动取值,别人教的
- HDU 3336 Count the string ( KMP next函数的应用 + DP )
- HDU 3336 Count the string(KMP:串前缀匹配自身+DP)
- HDU 3336 Count the string(KMP)||记忆化搜索
- HDU 3336 Count the string / KMP
- HDU 3336 Count the string(KMP+dp)
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string(KMP)
- Hdu 3336 Count the string[KMP next数组的理解]
- hdu 3336【Count the string】(KMP)
- hdu 3336 Count the string(KMP)
- HDU 3336 Count the string KMP
- HDU 3336 Count the string(KMP)
- [HDU 3336]Count the String[kmp][DP]
- Hdu 3336 Count the string (KMP+DP 前缀出现次数和)
- 【HDU】3336 Count the string KMP