[dp][KMP][hdu3336]Count the string_kmp

來源:互聯網
上載者:User
Count the string

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
HDOJ Monthly Contest – 2010.03.06

解題思路
dp[i]表示以s[i]前一個位置結尾可以得到的首碼串個數,因為你的s[f[i]]與s[i]時不同的,但是這個數組的的深層含義卻是0-f[i]-1的這個首碼會以s[i]-1結尾,中會能匹配到這個首碼。
我們得到的f數組,,,,
所以當f[i]==0時,說明前面沒有匹配項,dp[i]=1,及他本身
else dp[i]=dp[f[i]-1]+1;

代碼:

#include<iostream>#include<cstdio>#include<ctime>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endif#define INF 0x3f3f3f3f#define clock CLOCKS_PER_SEC#define cle(x) memset(x,0,sizeof(x))#define maxcle(x) memset(x,0x3f,sizeof(x))#define mincle(x) memset(x,-1,sizeof(x))#define maxx(x1,x2,x3) max(x1,max(x2,x3))#define minn(x1,x2,x3) min(x1,min(x2,x3))#define cop(a,x) memcpy(x,a,sizeof(a))#define FROP "hdu"#define C(a,b) next_permutation(a,b)#define LL long long#define smin(x,tmp) x=min(x,tmp)#define smax(x,tmp) x=max(x,tmp)using namespace std;const int mod=10007,N=200005;char s[N];int f[N];int dp[N];int T,n;void get_fail(){    f[0]=0,f[1]=0;    int j=0;    for(int i = 1; i < n; i++)    {        j=f[i];        while(j && s[j]^s[i])j=f[j];        f[i+1]=s[i]==s[j]?j+1:j;    }}int main(){    freopen(FROP".in","r",stdin);    freopen(FROP".out","w",stdout);    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        scanf("%s",s);        get_fail();        dp[0]=1;        int sum=0;        for(int i = 1; i <= n; i++)        {            if(!f[i])dp[i]=1;            else            dp[i]=dp[f[i]-1]+1;            sum=(sum+dp[i])%mod;        }        printf("%d\n",sum);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.