题目链接:https://codeforc.es/contest/1200/problem/E

题意:

有n串字符串,让你连起来:sample please ease in out   ---》 sampleaseinout

思路:

肯定KMP啊,但是比赛的时候对kmp的next数组一知半解,所以也不知道怎么用。

next数组其实就是对key串进行处理next(失配值),然后在母串上去匹配。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> https://codeforc.es/contest/1200/problem/E
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e7+; int nxt[N],tlen,klen;
char key[N],txt[N]; void getNext()
{
int j,k;
j=;
k=-;
nxt[]=-;
while(j<klen)
{
if(k==-||key[j]==key[k])
{
nxt[++j]=++k;
if(key[j]!=key[k])//优化
nxt[j]=k;
}
else
k=nxt[k];
}
} int kmp()
{
int i=max(tlen-klen,),j=;//这边的 i 由于是算前后最大匹配长度所以只需要从后面的 key 长度的段开始就行了;
getNext();
while(i<tlen)//以母串结束而告终;
{
if(j==-||txt[i]==key[j])
{
i++;
j++;
}
else
j=nxt[j];
}
return j;//出来的 j 就是 i 到结尾到时候的最大匹配长度;
} int main()
{
int n;
sc("%d",&n);
sc("%s",txt);
tlen=strlen(txt);
fo(i,,n)
{
sc("%s",key);
klen=strlen(key);
int pos=kmp();
fo(j,pos,klen-)
txt[tlen++]=key[j];
}
txt[tlen]='\0';
pr("%s\n",txt);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words的更多相关文章

  1. Codeforces Round #578 (Div. 2)

    Codeforces Round #578 (Div. 2) 传送门 A. Hotelier 暴力即可. Code #include <bits/stdc++.h> using names ...

  2. Codeforces Round #578 (Div. 2) Solution

    Problem A Hotelier 直接模拟即可~~ 复杂度是$O(10 \times n)$ # include<bits/stdc++.h> using namespace std; ...

  3. Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)

    题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...

  4. Codeforces Round #578 (Div. 2) 二维差分 可做模板

    题意: 在n*n的矩阵中,你可以选择一个k*k的子矩阵,然后将这个子矩阵中的所有B全部变为W,问你怎么选择这个子矩阵使得最终的矩阵中某一行全是W或者某一列全是W的个数最多 题解:考虑每一行和每一列,对 ...

  5. Codeforces Round #578 (Div. 2) C. Round Corridor (思维,数论)

    题意: 有一个分两层的圆盘,每层从12点方向均分插入\(n\)和\(m\)个隔板,当内层和外层的隔板相连时是不能通过的,有\(q\)个询问,每次给你内层或外层的两个点,判断是否能从一个点走到另外一个点 ...

  6. Codeforces Round #244 (Div. 2)D (后缀自己主动机)

    Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后 ...

  7. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

  8. 【题解】Codeforces Round #798 (Div. 2)

    本篇为 Codeforces Round #798 (Div. 2) 也就是 CF1689 的题解,因本人水平比较菜,所以只有前四题 A.Lex String 题目描述 原题面 给定两个字符串 \(a ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. [笔记]C++下的数组声明

    /* 例子来源于<C++ Primer> */ ]; //prts是含有10个整数类型指针的数组 ]; //错误,没有引用的数组 ]; //指向有10个整型元素数组的指针 ]; //引用有 ...

  2. 【知识库】-数据库_MySQL之高级数据查询:去重复、组合查询、连接查询、虚拟表

    简书作者:seay 文章出处: 关系数据库SQL之高级数据查询:去重复.组合查询.连接查询.虚拟表 回顾:[知识库]-数据库_MySQL之基本数据查询:子查询.分组查询.模糊查询 Learn [已经过 ...

  3. MySQL_(Java)使用JDBC向数据库中删除(delete)数据

    MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...

  4. 「CF442C」 Artem and Array

    题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...

  5. mumu 连接安卓studio

    近期开发一个项目,折腾了好久,编写完程序后,真机上运行没有异常,而在MuMu模拟器上运行,程序总是中断(MuMu模拟器连接不上,只能打包好apk再放到MuMu上测试,有点蠢萌...),没办法调试,就获 ...

  6. Mac Vmware NAT模式

    1.NAT模式原理 2.MAC上关于Vmware的配置 1)/Library/Preferences/VMware Fusion/networking MacBookPro:~ zhangxm$ vi ...

  7. 如何使用Heartbeat,组建一个高可用性的mysql集群

    转了好多次帖子,其实就是为了使用heartbeat来搭建mysql集群,网上很多都是用make来生成RPM来安装,我也找了很多资料,mysql 自带的cluster用户不满意,只能再次vmware虚拟 ...

  8. Async Task Types in C#

    Async Task Types in C# Extend async to support task types that match a specific pattern, in addition ...

  9. ios修改UIIMage大小

    /** * 改变图片的大小 * * @param img 需要改变的图片 * @param newsize 新图片的大小 * * @return 返回修改后的新图片 */ - (UIImage *)s ...

  10. c#阿里云短信验证码

    发送验证码 private static void SendAcs(string mobile, string templateCode, dynamic json, int ourid) { if ...