[kmp,不要过多调用strlen!!!] Codeforces 1200E Compress Words

题目:http://codeforces.com/contest/1200/problem/E
Compress Words
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Amugae has a sentence consisting of nn words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges "sample" and "please" into "samplease".

Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends.

Input

The first line contains an integer nn (1n1051≤n≤105), the number of the words in Amugae's sentence.

The second line contains nn words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ..., '9'). The total length of the words does not exceed 106106.

Output

In the only line output the compressed word after the merging process ends as described in the problem.

Examples
input
Copy
5
I want to order pizza
output
Copy
Iwantorderpizza
input
Copy
5
sample please ease in out
output
Copy
sampleaseinout

题意:

给你n个字符串,你需要按顺序连接这些字符串,如果当前的字符串的前缀和上一个字符串的后缀相同,则连接时要去掉当前字符串的前缀

思路:

一开始暴力,果断TLE,然后用kmp,还是TLE,改了几十次后发现是每次调用kmp和getnex时都调用用了strlen,增加了复杂度,去掉之后就AC了
为什么用kmp呢?因为kmp可以在文本串中找到模板串匹配的部分,现在要当前字符串的前缀匹配上一个字符串的后缀,我们就从上一个字符串的长度-当前字符串的长度的位置开始找,
因为一个字符串的前缀最长是他本身,所以当前字符串的前缀最多能匹配到这个位置(再往前当前字符串的长度就不够了),然后用kmp匹配,然后返回一个当前字符串的前缀与上一个字符串的后缀不匹配的位置(就是return j,j是当前字符串匹配的位置,如果t[i]==p[j]则j++,所以返回时j会指向不匹配的位置)

注意:

如果调用太多次strlen会TLE!!!

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e6+5;
 4 char ans[amn],in[amn];
 5 int n,nex[amn],len,tp;
 6 void getnex(char in[]){     ///不要在这调用strlen!!!,用已有的就好了,不然会T到自闭!!!
 7     nex[0]=nex[1]=0;
 8     for(int i=1;i<len;i++){
 9         int j=nex[i];
10         while(j&&in[i]!=in[j])j=nex[j];
11         nex[i+1]=in[i]==in[j]?j+1:0;
12     }
13 }
14 int kmp(char t[],char p[],int pos){     ///不要在这调用strlen!!!,用已有的就好了,不然会T到自闭!!!
15     getnex(in);
16     int j=0;
17     for(int i=pos;i<tp;i++){
18         while(j&&p[j]!=t[i])j=nex[j];
19         if(p[j]==t[i])j++;
20     }
21     return j;
22 }
23 int main(){
24     ios::sync_with_stdio(0);
25     cin>>n;
26     cin>>ans;
27     int pos;
28     tp=strlen(ans);     ///注意,如果调用太多次strlen会TLE!!!
29     for(int i=2;i<=n;i++){
30         cin>>in;
31         len=strlen(in);
32         pos=tp-len;     ///上一个字符串的长度-当前字符串的长度,因为一个字符串的前缀最长是他本身,他最多只能匹配到这个位置
33         for(int j=kmp(ans,in,pos);j<len;j++)
34             ans[tp++]=in[j];
35     }
36     for(int i=0;i<tp;i++)
37         printf("%c",ans[i]);
38     printf("\n");
39 }
40 /**
41 给你n个字符串,你需要按顺序连接这些字符串,如果当前的字符串的前缀和上一个字符串的后缀相同,则连接时要去掉当前字符串的前缀
42 一开始暴力,果断TLE,然后用kmp,还是TLE,改了几十次后发现是每次调用kmp和getnex时都调用用了strlen,增加了复杂度,去掉之后就AC了
43 为什么用kmp呢?因为kmp可以在文本串中找到模板串匹配的部分,现在要当前字符串的前缀匹配上一个字符串的后缀,我们就从上一个字符串的长度-当前字符串的长度的位置开始找,
44 因为一个字符串的前缀最长是他本身,所以当前字符串的前缀最多能匹配到这个位置(再往前当前字符串的长度就不够了),然后用kmp匹配,然后返回一个当前字符串的前缀与上一个字符串的后缀不匹配的位置(就是return j,j是当前字符串匹配的位置,如果t[i]==p[j]则j++,所以返回时j会指向不匹配的位置)
45 然后从这个位置开始把当前字符串后面的字符都加到上一个字符串后面,知道输入完毕,最后输出ans就行了
46 注意:如果调用太多次strlen会TLE!!!
47 **/

 

转载于:https://www.cnblogs.com/Railgun000/p/11425935.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值