Hdu 3336 Count the string(求给定字符串含前缀的数量)
2016-04-06 12:34
405 查看
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7470 Accepted Submission(s): 3457
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 <= n <= 200000)
Abab,前缀为a,ab,aba,abab
abab中共有六个子串是前缀a a ab ab aba abab
所以答案为6
方法一:
Kmp+dp
Dp[i]=dp[Next[i]]+1,dp[i]表示以i为结尾包含的字符串的数量。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1100000;
int n,m;
const int MOD=10007;
char pattern[maxn];
int Next[maxn],ans,dp[maxn];
bool solve(int x1,int y1,int x2,int y2){
for(int i=x1,j=x2;i<=y1,j<=y2;i++,j++)
if(pattern[i]!=pattern[j])
return false;
return true;
}
void get_next(){
int i=0,j=-1;
Next[0]=-1,dp[0]=0;
while(i<m){
if(j==-1||pattern[j]==pattern[i]){
j++,i++;
Next[i]=j;
dp[i]=dp[Next[i]]+1;
ans+=dp[i];
}
else
j=Next[j];
}
}
int main(){
int _;
scanf("%d",&_);
while(_--){
scanf("%d",&m);
scanf("%s",pattern);
ans=0;
get_next();
printf("%d\n",ans%MOD);
}
return 0;
}
方法二:
EKMP
Next[i]表示以i为开头的字符串与与0为开头的字符串的最大公共前缀。
#include<bits/stdc++.h> using namespace std; const int maxn=210000; char x[maxn]; int Next[maxn]; void pre_EKMP(int m){ Next[0]=m; int j=0; while(j+1<m&&x[j]==x[j+1]) j++; Next[1]=j; int k=1; for(int i=2;i<m;i++){ int p=Next[k]+k-1; int L=Next[i-k]; if(i+L<p+1) Next[i]=L; else{ j=max(0,p-i+1); while(i+j<m&&x[i+j]==x[j]) j++; Next[i]=j; k=i; } } } int main(){ int _,n; scanf("%d",&_); while(_--){ scanf("%d",&n); scanf("%s",x); pre_EKMP(n); int ans=0; for(int i=0;i<n;i++) ans+=Next[i]; printf("%d\n",ans%10007); } return 0; }
相关文章推荐
- IP地址判断有效性
- 面试题38 数字在排序数组中出现的次数
- (CF#257)B. Jzzhu and Sequences
- java回调机制
- Spark源码阅读@ListenerBus 的实现
- 修改Android工程的编码,GBK 变UTF-8
- javascript基础知识(数组)
- 数据库 设计三大范式
- FreeMarker 生成Java、mybatis文件
- 传感器--------接近传感器
- HDU 1160 FatMouse's Speed (dp, 最长子序列)
- [Lintcode] 搜索旋转排序数组 II Search in Rotated Sorted Array II
- FreeMarker 生成Java、mybatis文件
- Android 中的长度单位详解
- Easyui-ComboTree数据填充,递归。树形节点
- Java Web开发——Struts2 中.action和.do的奥秘
- 返回一个整数数组中最大子数组的和
- django之创建第11个项目-页面整合
- 第五周项目2-游戏中的角色类(2)
- 观察比较法