问题描述
众所周知,aekdycoin擅长字符串问题和数论问题。当给定一个字符串s时,我们可以写下该字符串的所有非空前缀。例如:
S:“ABAB”
前缀是:“A”、“AB”、“ABA”、“ABAB”
对于每个前缀,我们可以计算它在s中匹配的次数,因此我们可以看到前缀“a”匹配两次,“ab”也匹配两次,“ab a”匹配一次,“ab ab”匹配一次。现在,您需要计算所有前缀的匹配时间之和。对于“abab”,它是2+2+1+1=6。
答案可能非常大,因此输出答案mod 10007。

输入
第一行是一个整数t,表示测试用例的数量。对于每种情况,第一行是一个整数n(1<=n<=200000),它是字符串s的长度。后面的一行给出字符串s。字符串中的字符都是小写字母。

产量
对于每种情况,只输出一个数字:s mod 10007所有前缀的匹配时间总和。

对s进行lens次的分割  然后不断kmp  但是超时了

#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define N 200000+5
#define mod 10007
string s,p;
int ans;
int lens,lenp;
int nex[N];
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp-)
{
if(k==-||p[j]==p[k])
nex[++j]=++k;
else k=nex[k];
}
} int kmp()
{
int j=,i=;
while(i<lens&&j<lenp)
{
if(s[i]==p[j]||j==-)
{
i++;
j++;
}
else j=nex[j];
if(j==lenp)
{
ans++;j=;
}
}
} int main()
{
int cas;
RI(cas);
while(cas--)
{
RI(lens);
ans=;
cin>>s;
for(lenp=;lenp<=lens;lenp++)
{
p=s.substr(,lenp);
getnext();
kmp();
}
cout<<ans<<endl;
}
return ;
}

显然都200000了  必然会超时的

这题其实用不到kmp  主要是next数组

next数组才是kmp的精髓 !!!!!!

对p进行next扫描

初始化:cnt=lenp

如果next为0肯定没有可匹配的

如果next不为0 肯定有至少一个相匹配的

如果前后缀没有交集  肯定是一个匹配的

如果有交集  为1+交集中 相匹配的( 因为交集中的字符串  就是最后的字符串! 这点是本题最重要的  )

显然 交集的最后一个匹配值为囊括了交集中所有匹配的(而这个值之前已经遍历过了!)

详情见代码

#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define N 200000+5
#define mod 10007
string s,p;
int lens,lenp;
int nex[N];
int ans[N];
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp)
{
if(k==-||p[j]==p[k])
nex[++j]=++k;
else k=nex[k];
}
}
int main()
{
int cas;
RI(cas);
while(cas--)
{
RI(lenp);
cin>>p;
getnext();
int cnt=lenp;
rep(i,,lenp)
{
if(!nex[i])continue;
if(nex[i]*<=i)
{
cnt++; ans[i]=; continue;
}
cnt++;
cnt+=ans[nex[i]];
ans[i]=+ans[nex[i]];
}
cout<<cnt%mod<<endl;
}
return ;
}

Count the string kmp的更多相关文章

  1. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  2. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  4. 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 ...

  5. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  6. HDU3336 Count the string(kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. hud 3336 count the string (KMP)

    这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...

  8. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  9. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

随机推荐

  1. 【原创】运维基础之Docker(5)docker部署airflow

    部署方式:docker+airflow+mysql+LocalExecutor 使用airflow的docker镜像 https://hub.docker.com/r/puckel/docker-ai ...

  2. [MySql]GRANT权限的一些技巧

    运用 '; 可以快速创建一个用户拥有某表的SELECT操作: 运用 SHOW GRANTS FOR 'test_1'@'localhost'; 分析该用户的最终GRANT权限: MySql默认把tes ...

  3. SQL Server管理员必备技能之性能优化

    SQL Server管理员必备技能之性能优化 高文龙关注1人评论1171人阅读2017-09-22 08:27:41 SQL Server 作为企业必不可少的服务之一,所以对于管理员的日常运维是一个极 ...

  4. React-Native到0.44版本后Navigator 不能用的问题

    新升级  到0.46版本以后 Navigator 不能使用报错. 'Navigator is deprecated and has been removed from this package. It ...

  5. LeetCode(100):相同的树

    Easy! 题目描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 ...

  6. pyhon----函数和方法的关系

    1.如果使用类名调用,为函数,需要手动传self 2.如果使用对象调用,为方法,不用手动传self class Foo(object): def __init__(self): self.name=& ...

  7. bzoj 1951

    这道题告诉了我们一个很重要的道理:看到题,先想明白再动手! 题意:求对999911659取模的值 首先,由于n的数据范围不是很大(至少不是很大),所以可以O()枚举所有约数分别求组合数 但是有个问题: ...

  8. mac下Fiddler的安装-启动

    使用教程参考:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#request 环境安装 Mono安装 首先,Mac下需要 ...

  9. python发送邮件(在邮件中显示HTMLTestRunner生成的报告)

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart f ...

  10. C#学习-const和readonly

    const是表示为常量的关键字,一旦赋值就不能改变了.是程序编译时候CLR就将const的值编译到IL代码中了. readonly也是常量的关键的字: 所以,有了这两个关键字的比较.readonly肯 ...