问题描述
众所周知,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. django.db.utils.OperationalError: (1045, "Access denied for user 'ODBC'@'localhost' (using password)

    错误描述: 从SQLLITE数据库换为MYSQL数据库,执行 python manage.py migrate 命令时,报错:django.db.utils.OperationalError: (10 ...

  2. Golang并发模型之Context详解

    对于 Golang 开发者来说context(上下文)包一定不会陌生.但很多时候,我们懒惰的只是见过它,或能起到什么作用,并不会去深究它. 应用场景:在 Go http 包的 Server 中,每一个 ...

  3. 清北学堂 清北-Day3-R2-打架 (fight)

    题目描述 LYK有 \(n\) 个小朋友排成一排.第 \(i\) 个小朋友的战斗力是 $ a_i $,且他们的战斗力互不相同. 战斗力高的会打败战斗力低的. LYK想恶搞这些小朋友们,具体地,它有 \ ...

  4. boolalpha的作用

    #include <iostream>using namespace std;int main(){        bool b=true;        cout << &q ...

  5. HttpListener通讯成功案例

    1.创建WindowsService,如下代码 using System;using System.Net;using System.Net.Sockets;using System.ServiceP ...

  6. Confluence 6 Oracle 创建数据库用户

    创建用户后并且指派权限: 使用 sqlplus 命令行工具通过命令行来访问 Oracle sqlplus user/password <as sysdba|as sysoper> 如果你的 ...

  7. Confluence 6 使用一个主题到站点

    主题被用来在你的 Confluence 站点中应用表现形式.请查看 Working with Themes 页面来查看如何应用你的整个站点和如何添加更多的主题. 希望在站点中应用主题: 进入  > ...

  8. 自定义你的 Confluence 6 站点

    本部分对 Confluence 全站进行自定义的相关内容进行了说明.这部分只能是具有 Confluence 的管理员权限的用户才能进行操作 —— 系统管理员或者 Confluence 管理员. 有关对 ...

  9. Swift中使用oc代码桥接设置

    1 将oc的代码拖入项目中 2 新建一个头文件 在头文件中导入你想用的oc头文件  import "****.h" 3 在设置build Setting 中搜索bird 找到 Ob ...

  10. Jquery无刷新实时更新表格数据

    html代码: <style> .editbox { display:none } .editbox { font-size:14px; width:70px; background-co ...