hdu 3336 Count the string （nxt函数的运用kmp+（dfs|dp )）

Posted by 111qqz on Friday, August 12, 2016

TOC

hdu 3336 题目链接

s          a    b    a    b    a i          0    1    2    3    4   5 next[i]     -1   0    0    1    2   3

ans初始为len(因为长度为len的字符串有len个前缀，每个前缀至少出现一次) next[3] = 1，ans + 1 = 6，next[1] = 0 next[4] = 2，ans + 1 = 7，next[2] = 0 next[5] = 3，ans + 1 = 8，next[3] = 1，ans + 1 = 9

``````/* ***********************************************
Author :111qqz
Created Time :2016年08月12日 星期五 12时59分12秒
File Name :code/hdu/3336.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int MOD = 10007;
const int N=2E5+7;
int n;
char s[N];
int nxt[N];
int ans;
void getnxt(char *s)
{
int n = strlen(s);
int i = 0 ;
int j = -1;
nxt[0] = -1;
while (i<n)
if (j==-1||s[i]==s[j]) nxt[++i]=++j;
else j = nxt[j];
}
void dfs( int x)
{
if (x<=0) return ;
ans = (ans + 1) % MOD;
dfs(nxt[x]);
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
scanf("%s",s);
getnxt(s);
int len = strlen(s);
ans = len;
//for ( int i = 1 ; i <= len ; i++) cout<<"i:"<<i<<" "<<nxt[i]<<endl;
for ( int i = 1 ; i <= len ; i++)
if (nxt[i]>0) dfs(nxt[i]);//cout<<"ans:"<<ans<<endl;
printf("%d\n",ans);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

dp[i]表示长度为i的前缀出现的此处，显然每个前缀至少出现了一次，所以初始化dp[i]=1  (1=<i <= len)

nxt[i]表示的是字符串的0..i-1位中，前缀和后缀相等的串的最长长度为nxt[i]

``````/* ***********************************************
Author :111qqz
Created Time :2016年08月12日 星期五 12时59分12秒
File Name :code/hdu/3336.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int MOD = 10007;
const int N=2E5+7;
int n;
char s[N];
int nxt[N];
int ans;
void getnxt(char *s)
{
int n = strlen(s);
int i = 0 ;
int j = -1;
nxt[0] = -1;
while (i<n)
if (j==-1||s[i]==s[j]) nxt[++i]=++j;
else j = nxt[j];
}
int dp[N];
int main()
{
#ifndef  ONLINE_JUDGE
freopen("./in.txt","r",stdin);
#endif
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
scanf("%s",s);
getnxt(s);
int len = strlen(s);
for ( int i = 1 ; i <= len ; i++) dp[i] = 1;

for ( int i = len ; i >= 1 ; i--)
{
dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % MOD;
}
int ans = 0 ;
for ( int i = 1 ; i <= len ; i++) ans = (ans + dp[i])%MOD;
printf("%d\n",ans);
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」