Count the string

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

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

分析

  1. 如果枚举每一个前缀字串,然后KMP匹配的话,会超时,所以我们要利用KMP的特性来巧妙计算。
  2. KMP中的next数组表示的是最长公共前后缀字串长度,基于这一个特点。每次遍历到 i ,对于 next[i] 来讲,又重复出现了前缀长度为next[i] 的字串,基于这一特点,我们由前向后推出每一个 i 处的所有前缀重复数,所谓DP。
  3. dp[i] = (dp[nxt[i]]+1)%mod; 即状态转移方程。也就是说,长度为 i 的前缀串中又多了一份长度为nxt[i]的前缀字串的个数,再加上自身,就是更新得到的数量。
const int mod = 10007;
const int MAX = 200005;
int dp[MAX];
int n;
int nxt[MAX];
char a[MAX];
void getnxt()
{
nxt[0] = -1;
int j = 0,k=-1;
while(j<n)
{
if(k==-1||a[j]==a[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>n;
cin>>a;
getnxt();
long long ans = 0;
for(int i=1;i<=n;i++)
dp[i] = (dp[nxt[i]]+1)%mod;
for(int i=1;i<=n;i++)
ans = (ans+dp[i])%mod;
cout<<ans<<endl;
}
return 0;
}

HDU-3366-Count the string(KMP,DP)的更多相关文章

  1. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  2. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 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) ...

  4. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  5. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4588 Count The Carries (数学,计数)

    题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...

  7. HDU 5414 CRB and String (字符串,模拟)

    题意:给两个字符串s和t,如果能插入一些字符使得s=t,则输出yes,否则输出no.插入规则:在s中选定一个字符c,可以在其后面插入一个字符k,只要k!=c即可. 思路:特殊的情况就是s和t的最长相同 ...

  8. string (KMP+期望DP)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description  给定一个由且仅由字符 'H' , 'T' 构成的字符串$S$. 给定一个最初为空的字符串 ...

  9. Codeforces 126B. Password(KMP,DP)

    Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...

随机推荐

  1. vue——运行一个项目

    教程:https://segmentfault.com/a/1190000009871504 启动:cnpm run dev

  2. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

  3. Washing Plates 贪心

    https://www.hackerrank.com/contests/101hack41/challenges/washing-plates 给定n个物品,选这个物品,贡献 + p, 不选的话,贡献 ...

  4. pat1055. The World's Richest (25)

    1055. The World's Richest (25) 时间限制 400 ms 内存限制 128000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. devExpress GridControl gridView笔记

    gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(, , , ); gridView1.Appearance.OddRow.BackCol ...

  6. 框架使用-Sql拼接

     Sql语句拼写: 查询 DQueryDom DmoQuery(返回的整个对象) 更新 DQUpdateDom 删除 DQDeleteDom 条件 dom.Where.Conditions.Add(D ...

  7. UICollectionView笔记1

    WWDC 2012 Session笔记——205 Introducing Collection Views 这是博主的WWDC2012笔记系列中的一篇,完整的笔记列表可以参看这里.如果您是首次来到本站 ...

  8. java8 peek

    这样不会有任何的输出:Stream.of("one", "two", "three", "four").peek(e - ...

  9. 2 cmd中startup显示运行不了显示“不是内部或外部命令”

    解决方案: 1 在C:\Windows\System32中检查cmd.exe是否存在(如果存在的话)(检查cmd.exe是否被误删) 2 在我的电脑——属性——环境变量——在系统变量找到Path编辑前 ...

  10. Eucalyptus-instance启动后查看运行状态

    1.前言 在eucalyptus中通过虚拟机模板,创建并启动一个虚拟机,这个时候虚拟机启动正常,但是外部一直无法访问也ping不通,正对这种情况我们如何检查排除问题呢? 两种检查问题的方法: 1).在 ...