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. Hotspot GC研发工程师也许漏掉了一块逻辑

    本文来自: PerfMa技术社区 PerfMa(笨马网络)官网 概述 今天要说的这个问题,是我经常面试问的一个问题,只是和我之前排查过的场景有些区别,属于另外一种情况.也许我这里讲了这个之后,会成为不 ...

  2. 用 Python 制作关不掉的端午安康弹窗

    端午节又称端阳节.龙舟节.重午节.龙节.正阳节.天中节等,端午节源自天象崇拜,由上古时代祭龙演变而来,因传说战国时期的楚国诗人屈原在五月五日跳汨罗江自尽,后来人们亦将端午节作为纪念屈原的节日,在端午节 ...

  3. java final关键字与static关键字

    一  final关键字 1.final修饰类不可以被继承,但是可以继承其他类. 例如: class Yy {} final class Fu extends Yy{} //可以继承Yy类 class ...

  4. 快速入门Mybatis

    框架概述 什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题.使用框架的好处:框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开发效率 三层架构 UI(表现层 ...

  5. 【AI 算法评测】BERT 对 NLP 效果的改善,不负众望!

    AI 在各大领域的发展有目共睹,而作为人工智能皇冠上的明珠--自然语言处理却成果了了,大多实现或者以半成品的形式躺在实验室中,或者仅仅作为某个产品的辅助功能.而这一情况在 BERT 出现后出现了很大的 ...

  6. python的一些使用体会

    python刚开始接触,因为刚好有点需求,所以写了点小程序,一点点体会. 优点: 1. os.rename()方法不错,c#就没有这个方法 2.字符串的slice操作不错,取substring有时比较 ...

  7. Windows下nacos单机部分发现的坑

    一.下载nacos的地址: https://github.com/alibaba/nacos/releases 下载 nacos-server-1.3.2.tar.gz    就好 二.在Window ...

  8. DDD与Repository

    Repository已经不是什么新鲜概念了.DDD模型自2004年提出,发展至今已经16年了.但是不少企业却无法实施,其原因也很简单:DDD是基于需求的,而很多并不理解需求:DDD是容易实现的,而很多 ...

  9. Scala集合中的函数(扩展)

    1.拉链(zip) 将两个集合进行 对偶元组合并,可以使用拉链 object Demo_031 { def main(args: Array[String]): Unit = { val list1 ...

  10. 七脚OLED屏幕使用IIC接口

    7pin 0.96寸OLED模块支持SPI和IIC接口 默认是SPI接口;如果想用IC接口;操作如下几步骤: 1.将模块背面的电阻R3换到R1位置,此时将模块切换为IIC接口:电阻R8可以用0欧姆电阻 ...