zoukankan      html  css  js  c++  java
  • HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8845    Accepted Submission(s): 4104

    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

    题目链接:HDU 3336

    比较有意思的一题,因为Next[i]代表了字符串S[1~i](是1~i而不是i~len)中最长公共前后缀的长度同时也是最长公共前缀的结束位置,假设当前循环的到了第K个,那可以发现S[1~k]的前缀S[1~Next[k] ]与S[ (len-Next[k]+1)~K]是一样的。

    用cnt[i]表示以i结尾的前缀中所出现的所有重复数就有cnt[i]=cnt[Next[k] ]+1即前面重复过的次数加上此时的又出现一次重复

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<bitset>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=200010;
    const int mod=10007;
    char s[N];
    int nxt[N];
    int len;
    int dp[N];
    void getnext()
    {
    	CLR(nxt,0);
    	int j=0,k=nxt[0]=-1;
    	len=strlen(s);
    	while (j<len)
    	{
    		if(k==-1||s[j]==s[k])
    			nxt[++j]=++k;
    		else
    			k=nxt[k];
    	}
    }
    int main(void)
    {
    	int tcase,i,j,n,ans;
    	scanf("%d",&tcase);
    	while (tcase--)
    	{
    		ans=0;
    		CLR(dp,0);
    		scanf("%d%s",&n,s);
    		getnext();
    		for (i=1; i<=n; ++i)
    		{
    			dp[i]=dp[nxt[i]]+1;
    			ans=(ans+dp[i])%mod;
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
  • 相关阅读:
    java接口变量问题
    FileInputStream与BufferedInputStream的对比
    eclipse使用javaFX写一个HelloWorkld
    Windows 安装Java与配置环境变量
    asp.net core处理中文的指南
    修改release management client对应的服务器的地址
    在server2012安装tfs遇到的问题:KB2919355
    release management客户端无法连接到release management server的问题解决
    如何升级PowerShell
    VS2010下调试.NET源码
  • 原文地址:https://www.cnblogs.com/Blackops/p/5805538.html
Copyright © 2011-2022 走看看