hdu 3336 Count the string(next数组+dp)
来源:互联网 发布:逆矩阵怎么求 编辑:程序博客网 时间:2024/06/05 07:19
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
题目大意:
给一个字符串,输出包括从1到len长 字符串前缀的总个数
解法:
先计算next数组,设dp[i]表示子串b[0~i]共含有以b[i]为结尾的前缀的数目,则以b[i]结尾的前缀数就是自己本身加上以b[p[i]]结尾的前缀数。
#include<iostream>using namespace std;#define mod 10007char s[200005];int n,t,d[200005],nxt[200005];void getNext(){ int i,j=-1,ans=1; d[0]=1; nxt[0]=-1; for(i=1;i<n;i++) { while(j>=0&&s[i]!=s[j+1]) j=nxt[j]; nxt[i]=(s[i]==s[j+1])?j++:j; if(j>=0) d[i]=(d[j]+1)%mod; //如果nxt数组j不为负 dp当前状态d[i]决策累加d[j]的值 else d[i]=1; //否则加一即可 ans=(ans+d[i])%mod; } cout<<ans<<endl;}int main(){ cin>>t; while(t--) { cin>>n>>s; getNext(); } return 0;}
- HDU 3336 Count the string(next数组+DP)
- hdu 3336 Count the string(next数组+dp)
- HDU 3336 Count the string(KMP+稍微DP+next数组的运用)
- Count the string next数组+DP
- [KMP-NEXT数组特性]HDU 3336 Count the string
- Hdu 3336 Count the string[KMP next数组的理解]
- HDU 3336 Count the string (next数组活用)
- 【HDU 3336】Count the string 【KMP next数组巧妙应用】
- HDU 3336 Count the string(KMP算法next数组的应用)
- HDU 3336 Count the string(KMP+Next数组递推)
- HDU 3336 Count the string(kmp next数组的性质)
- hdu3336 Count the string(next数组)
- HDU 3336 Count the string 后缀数组 或 (KMP + DP)
- HDU 3336 Count the string(KMP+dp)
- [HDU 3336]Count the String[kmp][DP]
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string KMP+DP
- HDU 3336 Count the string kmp+dp
- ORA-01008 并非所有变量都已关联 ????
- ViewPager学习总结
- centos7 grub2 引导相关
- 水题解题报告01---校门外的树(POJ原2808)
- 责任链--基本模式
- hdu 3336 Count the string(next数组+dp)
- TCHAR赋值和比较的问题
- 归档错误
- Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- c# .net 4.0 HttpWebRequest 访问https TLS1.2 解决方案
- 数据库(一)
- MFC中MessageBox置于顶层的问题
- ReadProcessMemory函数读取基址时输出却变成了一个地址,请教一下大家
- hashCode 和 equals