Count the string
来源:互联网 发布:阿里云服务器如何建站 编辑:程序博客网 时间:2024/06/11 02:41
题目描述
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.
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.
输入描述:
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.
输出描述:
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
示例1
输入
14abab
输出
6
#include <cstdio>#include <cstring>int const MAX = 200000 + 5;int const MOD = 10007;int next[MAX];char s[MAX]; void get_next(){ int i = 0, j = -1; next[0] = -1; while(s[i] != '\0') { if(j == - 1 || s[i] == s[j]) { i++; j++; next[i] = j; } else j = next[j]; }} int main(){ int T, len, ans; scanf("%d", &T); while(T--) { memset(next, 0, sizeof(next)); scanf("%d %s", &len, s); get_next(); ans = len; for(int i = 1; i <= len; i++) { int tmp = next[i]; while(tmp) { ans = (ans + 1) % MOD; tmp = next[tmp]; } } printf("%d\n", ans); }}
阅读全文
0 0
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- hdu3336-Count the string
- Count the string + KMP
- hdu3336 Count the string
- Count the string (KMP)
- Count the string
- hdu3336 Count the string
- hdu3336----Count the string
- hdu3336 Count the string
- Count the string
- 【HDU3336】【Count the string】
- Count the string
- Count the string
- HDU3336 Count the string
- hdu3666 Count the string
- 3336 Count the string
- myeclipse启动maven项目报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoader
- java内部类详解
- mt2503[CB]MODEM里与CB有关的NVRAM,以及每个NVRAM LID的作用是什么
- [odoo]替换Powered by信息
- ORACLE 报错ORA-00439、ORA-01078、LRM-00109、ORA-38760的解决方法
- Count the string
- laravel 目录结构
- 从勒索软件到工控系统网络安全
- nodejs和npm安装
- 正负数取反
- 做机械臂导航时遇到的问题5:如何使用ROS内嵌serial功能包实现串口通信
- java_文件名排序
- nuttx飞控操作系统
- Docker认识