hdu 3336 Count the string KMP+DP优化
Count the string
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.
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.
4
abab
#include <bits/stdc++.h>
using namespace std;
#define mod 10007
const int maxn=2e5+;
int f[maxn];
char s[maxn];
void getfail(char *T)
{
int i=,j=,n=strlen(T+);f[]=;
while(i<n){
if(j==||T[i]==T[j])//确定当前位,给下一位一个机会
++i,++j,f[i]=j;
else
j=f[j];
}
}
int main()
{
int T,n;
cin>>T;
while(T--)
{
scanf("%d%s",&n,s+);
getfail(s);
int cnt=;
for(int i=;i<=n;i++){
int t=;
for(int j=i;j;j=f[j])
if(s[i]==s[j]) // 相等才累加;
++t;
cnt=(cnt+t)%mod;
}
cout<<cnt<<endl;
}
return ;
}
2.上面的是正版的KMP...对于每一个f[i]其实只是一个试探可能性,并不确定是否相等;那么我们就利用前面的相等的f[i]来确定与当前位相等的f[i],这样就不需要病了f[i]来找是否p[i] = p[f[i]]了,并且这是一个基础,即一个子结构。用一个数组记录下前一个p[f[i]]的前缀串,直接+1即可;
62ms,时间复杂度为O(n)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+;
const int mod = ;
char p[N];
int f[N],dp[N];
void getfail()
{
int j=,n=strlen(p+);
f[]=;
for(int i = ;i <= n;i++){
while(j && p[j+] != p[i]) j = f[j];//确定当前位置是前面前缀串的最后一位;
if(p[i] == p[j+]) j++;
f[i] = j;
}
}
int main()
{
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s", p+);
getfail();
int ans = ;
dp[] = ;
for(int i = ;i <= n;i++){
dp[i] = dp[f[i]] + ;
ans = (ans+dp[i])%mod;
}
printf("%d\n",ans);
}
return ;
}
hdu 3336 Count the string KMP+DP优化的更多相关文章
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- [HDU 3336]Count the String[kmp][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- HDU 3336 Count the string ( KMP next函数的应用 + DP )
dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 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) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
随机推荐
- [Effective C++ --011]在operator=中处理“自我赋值”
一.何谓“自我赋值”? 1.1.场合一 直接赋值 w = w; 1.2.场合二 同一数组 a[i] = a[j]: 1.3.场合三 指针 *px = *py: 1.4. ...
- Java设计模式09:单例模式的强化(控制实例个数n)
1. 单例模式的本质: 控制实例数目(目的节约资源) 2. 单例模式体现的一些思想: (1)延迟装载(Lazy Load):懒汉式 (2)缓存:饿汉式 3. 单例模式的变形使用: 控制使用实例个数为3 ...
- Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器
1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...
- oracle 不转义 &
在为表加注释,遇到有些注释包含'&' 但又不想写eacape, 在sqlplus下,set define off 即可将&输入到数据库中.
- [转载]PHP 字符串替换中文
$a = "Car 神"; $result = preg_replace('/([\x80-\xff]*)/i','',$a); var_dump($result); 参考链接:p ...
- oracle交集,并集,差集
引自:http://www.2cto.com/database/201308/238777.html [sql] create table test1 ( name ), NN ) ); insert ...
- SharePoint移动客户端对比 ---Rshare 无疑是最好用的
目前市面上SharePoint移动客户端确实不少,但经过使用后的对比,Rshare无论在界面上还是在操作性上都占据了优势.大家可以下载进行尝试.
- Office365 InfoPath 表单的设计和应用(原创)
表单的应用:我想到的有2种. 1 做为自定义表单库的模版. 通过发放url(模版链接)给用户来填写表单. 最后将在表单库中得到所有填写的信息列表. 如 2 上传表单做为ContentType 也就是自 ...
- nginx支持url的PATHINFO
fastcgi_split_path_info ^(.+?\.php)(/.*)$; set $path_info $fastcgi_path_info; fastcgi_param PATH_INF ...
- 20160611-20160714springmvc入门进阶
springmvc第二阶段 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器 ...