HDU——T 3336 Count the string
2024-08-31 16:53:16
http://acm.hdu.edu.cn/showproblem.php?pid=3336
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10917 Accepted Submission(s): 5083
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.
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.
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
4
abab
Sample Output
6
Author
foreverlin@HNU
Source
Recommend
题意:求出每个前缀在串中出现的次数
每次统计答案时 增加 此串next[i]位置的次数+1即 f[i]=f[next[i]]+1,ans+=f[i]
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int mod();
int l,ans,p[N],f[N];
char s[N]; inline void Get_next()
{
for(int i=,j=;i<=l;i++)
{
for(;s[j+]!=s[i]&&j>;) j=p[j];
if(s[i]==s[j+]) j++;
p[i]=j;
}
} inline void init()
{
ans=;
memset(p,,sizeof(p));
memset(f,,sizeof(f));
} int main()
{
int t; scanf("%d",&t);
for(;t--;init())
{
scanf("%d%s",&l,s+);
Get_next();
for(int i=;i<=l;i++)
{
f[i]=(f[p[i]]+)%mod;
ans=(ans+f[i])%mod;
}
printf("%d\n",ans);
}
return ;
}
最新文章
- PHP 正则表达式 修饰符
- opencv 基础语法
- 我和Markdown故事
- javascript自定义滚动条插件,几行代码的事儿
- x-code快捷键
- Arduino 学习
- (C/C++) Interview in English. - Memory Allocation/Deallocation.
- SAP MRP的计算步骤
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
- 关于Linux的总结(三)
- jQuery 定时局部刷新(setInterval)方法总结
- iOS textfield实现一行的数字限制,超出进行弹框
- 转:推荐!国外程序员整理的 C++ 资源大全
- Redshift扩容及踩到的坑
- 制作jar包
- JavaScript Math(数学对象)
- [TFRecord文件格式]基本介绍
- linux-shell系列8 netstat用法
- Nginx+Flume+Hadoop日志分析,Ngram+AutoComplete
- skimage 安装和子模块