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;
    }
  • 相关阅读:
    在Visual Studio 2019中配置OpenCV环境
    Java中的垃圾回收
    线程池
    Java中锁优化
    二叉树的几种遍历
    java中Comparator的用法(排序、分组)
    java8 stream
    Java后台生成二维码并上传到阿里云OSS
    代码生成器的成长过程
    软件的军工六性
  • 原文地址:https://www.cnblogs.com/Blackops/p/5805538.html
Copyright © 2011-2022 走看看