题目链接: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. How to correctly set application badge value in iOS 8?

    o modify the badge under ios8 you have to ask for permissions let settings = UIUserNotificationSetti ...

  2. zookeeper系列(一)zookeeper图形化的客户端工具

    追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...

  3. Zookeeper执行原理的详细概述

    文章作者:Holy Null,来源:http://holynull.leanote.com/post/Zookeeper,非常感谢作者提供如此优秀的原创文章,作者通过俩个月的努力将<Hadoop ...

  4. linux安装vlc视频播放器

    文章来自转发 最近,打算在centos7.2上安装一个叫MPlayer的视频播放器,但是折腾好久,得到的结果只是可以播放,但是却没有声音.无奈之下另寻他路.最后选择安装VLC视频播放器. 我的linu ...

  5. vxworks 开发环境搭建

    育儿 分类: 嵌入式开发 VxWorks操作系统是美国WindRiver公司于1983年设计开发的一种嵌入式实操作系统 windriv vxwork时操作系统(RTOS),它以其良好的可靠性和卓越的实 ...

  6. Python中导入类

    python导入类与导入函数,模块基本一样,一个模块fun,其中包含三个类 class Dog(): def __init__(self,name): self.name=name def bark( ...

  7. js如何获取鼠标位置

    获取鼠标位置,首先需要加载js文件: 然后设置一个div,给定大小: 最后进行具体操作: //首先要先设置一个div,给定大小 <div id="m"></div ...

  8. layui弹出层处理(获取、操作弹出层数据等)

    要点: 字符串被渲染为弹窗层之后,回自动转换为DOM,可以使用jq进行各种操作 <!DOCTYPE html> <html> <head> <meta cha ...

  9. python programming作业10(仍有一点点小bug)

    # -*- coding: utf-8 -*- import os import platform import sys from PyQt5.QtCore import * from PyQt5.Q ...

  10. redis-trib.rb创建Redis集群时失败报错解决方案

    问题描述: [root@eshop-cache01 init.d]# redis-trib.rb create --replicas 1 192.168.1.110:7001 192.168.1.11 ...