Problem Description
String matching is a common type of problem in computer science. One string matching problem is as following:

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.

 
Input
The first line contains an integer T, denoting the number of test cases.
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

 
Output
For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.
 
Sample Input
3
_Happy_New_Year_
ywwyww
zjczzzjczjczzzjc
 
Sample Output
17
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)的更多相关文章

  1. [2019杭电多校第五场][hdu6629]string matching(扩展kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...

  2. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  3. Kattis - String Matching(kmp)

    String Matching Input The input consists of several test cases. Each test case consists of two lines ...

  4. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  5. 【ACM】Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  7. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

  8. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  9. HDU 4300 Clairewd's message ( 拓展KMP )

    题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...

随机推荐

  1. fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题

    fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题

  2. SqlServer 多表连接、聚合函数、模糊查询、分组查询应用总结(回归基础)

    --exists 结合 if else 以及 where 条件来使用判断是否有数据满足条件 select * from Class where Name like '%[1-3]班' if (not ...

  3. iptables看门狗

    近来业内很多服务器因redis造成服务器被黑,这个攻击的防范重点在于防火墙!! 有时为了方便我们可能会将iptables临时关闭,方便完倘若忘记把它打开,黑客大摇大摆就走进来. 这时候,我们需要条看门 ...

  4. 靶机练习 - ATT&CK红队实战靶场 - 1. 环境搭建和漏洞利用

    最近某个公众号介绍了网上的一套环境,这个环境是多个Windows靶机组成的,涉及到内网渗透,正好Windows和内网渗透一直没怎么接触过,所以拿来学习下. 下载地址:http://vulnstack. ...

  5. 源代码管理工具 ——Git的介绍与简要教程

    一.Github与Git (一)简介 GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. GitHub于2008年4月10日正式上线 ...

  6. JS实例-DOM查询

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  7. troubleshoot之:GC调优到底是什么

    目录 简介 那些GC的默认值 GC的选择 GC的最大线程个数 初始化heap size 最大的heap size 分层编译技术 我们到底要什么 最大暂停时间 吞吐率 简介 我们经常会听到甚至需要自己动 ...

  8. Jmeter 常用函数(1)- 详解 __Random

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 产生一个随机数 语法格式 ${__Ra ...

  9. Excel提取身份证出生日期②

    问题场景 从user表中的身份信息中提取用户的出生日期: 以下方法也可适用于提取其他数据,不仅是身份证信息: 以下图中数据都为测试数据,不具备真实性! 场景一 user表中的18位身份证,提取出生日期 ...

  10. anaconda下载包时网络连接错误的解决方法(CondaHTTPError:HTTP 000 connection failed for url)

    继上一篇<在WSL上搭载python编程环境>之后,下载软件和创建新环境的过程非常艰辛,下载太慢,以至于常常中断. 不论用conda安装一些python的包,还是创新独立的编程环境时,出现 ...