题意:http://acm.hdu.edu.cn/showproblem.php?pid=2594

如题。

思路:

Next数组记录的是pos位置失配时要跑到哪里,所以最后得再添加一个字符‘#’。

连结两个串的中间加一些奇怪字符以保证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>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#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 <cassert>
#include <iomanip>
//#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
//******************
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint 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
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int key[N];
int nxt[N]; void getNext(int klen)
{
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];
}
}
template<class T>
void PR(T _[],int n)
{
for(int i=;i<=n;++i)
{
cout<<_[i];
if(i==n)cout<<endl;
else cout<<' ';
}
}
char s1[N],s2[N];
int main()
{
while(~sc("%s",s1))
{
sc("%s",s2);
int len=strlen(s1);
s1[len++]='#';s1[len++]='%';
s1[len]='\0';
strcat(s1,s2);
len=strlen(s1);
s1[len++]='$';
s1[len]='\0';
for(int i=;i<len;++i)
key[i]=s1[i];
getNext(len);
int ans=nxt[len-];
if(s1[ans]==s1[len-])ans++;
if(ans==)
{
pr("0\n");
continue;
}
int len2=strlen(s2);
pr("%s %d\n",s2+len2-ans,ans);
// PR(nxt,len-1);
}
return ;
} /**************************************************************************************/

kmp跑两串的最大相同前后缀 HDU2594的更多相关文章

  1. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  2. HDU 2594(求最长公共前后缀 kmp)

    题意是在所给的两个字符串中找最长的公共前后缀,即第一个字符串前缀和第二个字符串后缀的最长相等串. 思路是将两个字符串拼接在一起,然后直接套用 kmp 算法即可. 要注意用 next 会报编译错误,改成 ...

  3. 利用KMP算法解决串的模式匹配问题(c++) -- 数据结构

    题目: 7-1 串的模式匹配 (30 分) 给定一个主串S(长度<=10^6)和一个模式T(长度<=10^5),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串 ...

  4. Objective-C 【NSString-字符串比较&前后缀检查及搜索】

    ———————————————————————————————————————————NSString 字符串比较 #import <Foundation/Foundation.h> vo ...

  5. poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14106   Ac ...

  6. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  7. Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

    #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2; ...

  8. OKR-Periods of Words【KMP最小前后缀】

    OKR-Periods of Words 传送门:链接    来源:UPC 8180 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得 ...

  9. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

随机推荐

  1. 自制 yum 源仓库

    转自:https://www.cnblogs.com/marility/p/7278374.html 背景 客户服务器为内网机器,centos7系统,且无法与外网连接.需要部署对应的LANMP环境及其 ...

  2. table表格整体居中

    代码: <div style="text-align:center"> <table border="1" cellpadding=" ...

  3. springboot连接mongodb进行CRUD

    springboot连接mongodb进行CRUD的过程: 在执行以下操作前已安装了mongodb并创建了用户和数据库,使用Robo 3T可成功连接. 1.创建springboot项目,加入以下mav ...

  4. ntp时间同步服务器的搭建

    CentOS系统一般自带安装有ntp服务,仅需做相关配置即可. 一.配置ntp服务器: 在选定的ntp服务器上vim /etc/ntp.conf 添加一行:restrict default nomod ...

  5. 《你不知道的JavaScript(上)》笔记——函数作用域和块作用域

    关于函数声明:如果 function 是声明中的第一个词, 那么就是一个函数声明, 否则就是一个函数表达式.例如匿名函数这种形式,函数会被当作函数表达式而不是一个标准的函数声明来处理. (functi ...

  6. LC 986. Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  7. Tomca的启动与关闭

    点击startup.bat启动,遇到一闪而过的问题,可能尚未配置JAVA_HOME 8080端口被占用导致启动失败 关闭Tomcat的三种方式 * 点击x (不推荐) * 双击shutdown.bat ...

  8. java time

    package cn.itcast_04;       import java.text.SimpleDateFormat;   import java.util.Date;       public ...

  9. SQL语句中有关单引号、双引号和加号的问题

    字符串数据是用单引号包在外面的,而+号只是用来连接这些字符串的. 数据库里的字段是整型的时候不要加单引号,是字符串的时候要加,其它类型根据实际情况来,双引号就是用来拼接字符串的,单引号是sql文的固有 ...

  10. Qt编写自定义控件36-图片浏览器

    一.前言 本控件主要用来作为一个简单的图片浏览器使用,可以上下翻页显示图片,图片还可以开启过度效果比如透明度渐变,应用场景有查看报警图片运行图片等.此控件非本人原创,来源于网络,我只是修正了好多处BU ...