string matching(拓展KMP)
Given a string s[0…len−1], please calculate the length of the longest common prefix of s[i…len−1] and s[0…len−1] for each i>0.
I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.
Each test case contains one string in a line consisting of printable ASCII characters except space.
* 1≤T≤30
* string length ≤106 for every string
_Happy_New_Year_
ywwyww
zjczzzjczjczzzjc
7
32
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e6+;
typedef long long ll;
using namespace std; ll ans;
void pre_EKMP(char x[],int m,ll next[])
{
next[]=m;
int j=;
while(j+<m&&x[j]==x[j+])
{
j++;
} next[]=j;
int k=;
for(int i=;i<m;i++)
{
int p=next[k]+k-;
int L=next[i-k];
if(i+L<p+)next[i]=L;
else
{
j=max(,p-i+);
while(i+j<m&&x[i+j]==x[j])j++; next[i]=j;
k=i;
}
}
} char str[*maxn];
ll nxt[*maxn];
int main()
{
int T;
cin>>T; while(T--)
{
scanf("%s",str);
int len=strlen(str);
ans=; pre_EKMP(str,len,nxt);
for(int t=;t<len;t++)
{
if(nxt[t]+t<len)
{
ans+=(nxt[t]+);
}
else
{
ans+=(len-t);
}
}
// ll ans=0;
printf("%lld\n",ans);
}
return ;
}
string matching(拓展KMP)的更多相关文章
- [2019杭电多校第五场][hdu6629]string matching(扩展kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Kattis - String Matching(kmp)
String Matching Input The input consists of several test cases. Each test case consists of two lines ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- 【ACM】Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- HDU 4300 Clairewd's message ( 拓展KMP )
题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...
随机推荐
- JS中的数组复制问题
JS中的数组复制问题 前言 首先提到复制,也就是拷贝问题,就必须要明确浅拷贝和深拷贝. 浅拷贝:B由A复制而来,改变B的内容,A也改变 深拷贝:B由A复制而来,改变B的内容,A的内容不会改变 总的来说 ...
- Bytom 储蓄分红 DAPP 开发指南
储蓄分红DAPP 储蓄分红合约简介 储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润.用户可以在 ...
- SSH安全免密码登录:ssh key
ssh key 使用非对称加密方式生成公钥和私钥 私钥存放在本地~/.ssh目录 公钥可以对外公开,放在服务器的~/.ssh/authorized_keys 1.linux平台生成ssh key ss ...
- XCTF-WEB-新手练习区(9-12)笔记
9:xff_referer X老师告诉小宁其实xff和referer是可以伪造的. 界面显示需要我们 添加X-Forwarded-For:123.123.123.123 添加Rerferer:http ...
- java 启动Tomcat报错:The specified JRE installation does not exist
启动TomCat服务报错: The specified JRE installation does not exist 解决方法: Eclipse:window->perferences-> ...
- java循环语句while与do-while
一 while循环 while循环语句和选择结构if语句有些相似,都是根据条件判断来决定是否执行大括号内的执行语句. 区别在于,while语句会反复地进行条件判断,只要条件成立,{}内的执行语句就会执 ...
- realm数据库报错:Changing Realm data can only be done from inside a transaction.
在编写realm数据库相关时: 代码: List<Student> delByStudent(String priNum){ RealmResults<Student> stu ...
- Jenkins配置总结
1.配置全局 系统管理->全局工具配置 2.配置 自己安装安装jdk,git,以及maven 3.系统管理->系统配置 3.1配置Jenkins URL 3.2 配置SSH Servers ...
- docker简记
title: docker学习简记 date: 2019-10-16 15:10:39 tags: docker Docker简记 1:Docker简介 1)出现背景 一款产品从开发到上线,从操作系统 ...
- 彻底搞懂https原理
我终于彻底理解了https原理!!!激动之下,写一篇博客,搞一波分享!!! 本篇博客比较精彩的地方: 思维方式:也是借鉴一位大佬的,写得很棒.https://blog.csdn.net/guolin_ ...