当前位置:网站首页>Summary 20220208 (KMP)
Summary 20220208 (KMP)
2022-07-22 18:46:00 【Vegetable chicken with konjaku】
I saw it this morning kmp The idea of algorithm , Is not very good , I checked some more this afternoon kmp The template of , The specific steps are not very clear , But I made a question based on the template . Take a closer look at the details tomorrow .
Compress Words
Title Description
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 format
The first line contains an integer nn ( 1 \le n \le 10^51≤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 10^6106 .
Output format
In the only line output the compressed word after the merging process ends as described in the problem.
Title Translation
Amugae Yes n Word , He wants to put this n Words become a sentence , Specifically, it is to merge two words into one word from left to right . When merging two words , To find the biggest i(i\ge 0)i(i≥0), The length of the first word is ii The suffix of and the length of the second word are ii The prefix of is equal , Then put the second word ii The part after bit is connected to the first word . Output the last word
I/o sample
Input #1 Copy
5 I want to order pizza
Output #1 Copy
Iwantorderpizza
Input #2 Copy
5 sample please ease in out
Output #2 Copy
sampleaseinout
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int t[N];
char s[N],p[N];
int len1,len2;
void next(char p[]) // Find prefix table array t
{
int j,i;
j=-1;
t[0]=-1;
i=0;
while(i<len2-1)
{
if(j==-1||p[i]==p[j])
{
i++;
j++;
if(p[i]!=p[j]) t[i]=j;
else t[i]=t[j];
}
else
j=t[j];
}
}
int kmp(char s[],char p[])
{
int i,j;
i=max(0,len1-len2);
j=0;
next(p);
while(i<len1)
{
while(s[i]!=p[j])// When it doesn't match , Begin to think about next Array
{
if(t[j]==0)// If there is no prefix in the substring , Substrings match from the beginning
{
j=0;
break;
}
else// The substring has the largest same prefix ,j from next[j] Start matching again at
j=t[j];
}
if(s[i]==p[j]) j++;
i++;
}
return j; // Go through the parent string to find the location of the child string
}
int main()
{
int n,i,j;
cin>>n;
cin>>s;
n-=1;
len1=strlen(s);
while(n--)
{
cin>>p;
len2=strlen(p);
int c=kmp(s,p);
i=len1-c;
for(j=0;j<len2;j++)
{
s[i++]=p[j]; // Add the new string after the long string
}
s[i]='\0';
len1+=len2-c;
}
cout<<s;
return 0;
}
边栏推荐
- Toss Phoenix system (by quqi99)
- 总结20220215(kruskal和prim)
- MySQL实现从其他表查询数据并插入另外一张表
- centos7.5下添加gd库然后mysql拓展库没了mysql拓展的配置也没问题,phpinfo中就是没有mysql拓展
- Go 内存模型
- Internet download manager2022 intelligent win latest version Downloader
- LeetCode:626. 换座位
- Add the GD Library under centos7.5, and then the MySQL expansion library. There is no problem without the configuration of MySQL expansion. There is no MySQL expansion in phpinfo
- no input file specified 解决方案
- 07.合成复用原则(Composite/Aggregate Reuse Principle,CARP)
猜你喜欢
[STM32] STM32 SDIO SD card read / write test (IV) -- SD_ Transfer mode phase of test
App mobile terminal test [6] application program (APK) package management and activity
ps: 如何调出辅助线
Mail Informer
【STM32】STM32 SDIO SD卡读写测试(一)-- SD卡硬件设计和软件移植
【FatFs】FAT32文件系统协议总结(理论+实践)
05. Law of Demeter LOD
【SDIO】SD2.0协议分析总结(三)-- SD卡相关命令介绍
【FatFs】基于STM32 SD卡移植FatFs文件系统
TCP与UDP及三次握手,四次挥手
随机推荐
【FatFs】FAT32文件系统协议总结(理论+实践)
Go 语言重要知识点:字符串、UTF-8 编码、rune
No input file specified solution
【STM32】STM32 SDIO SD卡读写测试(三)-- SD_Init之Init Card阶段
OSI模型,TCP/IP模型
使用工厂的方法创建对象
【TOOLS】TortoiseSVN如何设置比较工具为Beyond Compare 4
手写shallowReadonly和readonly
Try kolla-ansible (by quqi99)
The routing interface of local access to local TP5 of wechat applet is normal. Why can't you scan the code on the mobile phone to preview and get data?
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so'
总结20220209
ip,子网掩码,网关,IPS与IDS
包装类和字符串的方法
set up ovn based sr-iov test env (by quqi99)
【FatFs】基于STM32 SD卡移植FatFs文件系统
04. interface aggregation principle
03.单一职责原则(Simple Responsibility Pinciple)
Js高级-词法作用域
Go 语言学习:Go 语言之旅(三)