string matching

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3131    Accepted Submission(s): 724

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

题意:

给一个字符串s[0...len-1],计算它 s[i…len−1] 和 s[0…len−1] 最长公共前缀的长度之和,如果最后一个匹配的不是串中的最后一个,则要额外加1

思路:

用ex-kmp求解

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e6+;
char a[amn],str1[amn],str2[amn];
int pos[amn],ex[amn],alen,blen;
void f(){
pos[]=alen;
int x=;
while(str2[x]==str2[x+]&&x+<=blen)x++;
pos[]=x-;
int k=;
for(int i=;i<=alen;i++){
int p=k+pos[k]-,le=pos[i-k+];
if(i+le<p+)pos[i]=le;
else{
int j=p-i+;
if(j<)j=;
while(str2[j+]==str2[i+j]&&i+j<=blen)j++;
pos[i]=j;
k=i;
}
}
x=;
while(str1[x]==str2[x]&&x<=blen)x++;
ex[]=x-;
k=;
for(int i=;i<=alen;i++){
int p=k+ex[k]-,le=pos[i-k+];
if(i+le<p+)ex[i]=le;
else{
int j=p-i+;
if(j<)j=;
while(str2[j+]==str1[i+j]&&i+j<=alen&&j<=blen)j++;
ex[i]=j;
k=i;
}
}
}
int main(){
int T;
long long ans;
scanf("%d",&T);
while(T--){
scanf("%s",a);
alen=blen=strlen(a);
strcpy(str1+,a);
strcpy(str2+,a);
f();
ans=;
for(int i=;i<=alen;i++){
if(alen-i+>ex[i]){
ans+=ex[i]+;
}
else ans+=ex[i];
}
printf("%lld\n",ans);
}
}
/***
给一个字符串s[0...len-1],计算它 s[i…len−1] 和 s[0…len−1] 最长公共前缀的长度之和,如果最后一个匹配的不是串中的最后一个,则要额外加1
用扩展kmp求解
***/

最新文章

  1. [threeJs][新浪股票api][css3]3D新浪财经数据-最近A股涨的也太疯了......
  2. centos 如何清理/dev/vda1系统盘
  3. iOS 利用webView加载html代码,在代理中获取html页面的链接时出现的问题
  4. Linux crontab命令的使用方法
  5. jQuery类库的设计
  6. 解决asp.net mvc中*.resx资源文件访问报错
  7. android在myeclipse上创建的项目各种报错
  8. 201521123105 第9周Java学习总结
  9. Servlet第三篇【request和response简介、response的常见应用】
  10. xBIM 日志操作
  11. JVM 性能调优监控工具
  12. all unicode
  13. C#连接基于Java开发IM——Openfire
  14. 如何设置PDF签名文档,PDF签名文档怎么编辑
  15. spring boot项目基本结构
  16. MAC上截图,编辑图片与恢复图片
  17. Dart Map&lt;&gt; 添加 元素
  18. Mysql 多字段去重
  19. 解决 emoji表情存入数据库为&#39; ??? &#39;
  20. ps 和 grep 查找消除 grep自身查找

热门文章

  1. 基于SR-IOV的IO虚拟化技术
  2. RocketMQ borker配置文件
  3. 读书笔记&mdash;&mdash;《在线》
  4. Vue数据绑定(一)
  5. Spring Boot2.x 动态数据源配置
  6. linux入门系列16--文件共享之Samba和NFS
  7. jmap的使用以及内存溢出分析
  8. 正式学习MVC 05
  9. 【tomcat系列】配置tomcat远程访问
  10. R自带数据集