题意: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. vue中使用echarts画饼状图

    echarts的中文文档地址:https://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20EC ...

  2. 三十、CentOS 7之systemd

    一.系统启动流程 POST --> bootloader  --> MBR工作 --> kernel(initramfs/initrd) --> ro rootfs --> ...

  3. Docker容器数据券

    1.是什么? 2.能干嘛? 3.数据卷 ——容器内添加 方式一:直接命令添加 1).命令 2).在宿主机上新建并添加内容 3).查看容器内相应共享文件夹,发现宿主机的文件夹下发生变化,会同步到容器内的 ...

  4. navicat连接远程数据库报错'client does not support authentication protocol requested by server consider ...'解决方案

    [1.cmd终端连接远程mysql数据库方法] mysql -uhello -pworld   -h192.168.1.88 -P3306 -Dmysql_oa mysql -u用户名 -p密码 -h ...

  5. Sublime Markdown预览插件安装流程

    使用方法 在sublime中已编辑好的markdown使用快捷键 Alt+M 即可在浏览器预览效果. 需要安装的插件 Markdown Editting:主要用来做 Markdown 编辑时的语法高亮 ...

  6. HTTP之基本认证机制

    1. 认证 1.1 HTTP 的质询/响应认证框架 HTTP 提供了一个原生的质询/响应(challenge/response)框架,简化了对用户的认证过程. HTTP 的认证模型如下图所示: Web ...

  7. 五一 DAY 4

    DAY 4    2019.5.1 PART 1    进制转化 10 = 23+21= 1010(2)       = 32+30= 101(3) 进制转化常见问题: 1.十进制数 x ----&g ...

  8. git如何压栈某一个文件?

    答: 使用git stash -p进行交互式操作,y表示压栈,n表示不压栈

  9. 为什么使用 Web Services?

    最重要的事情是协同工作 由于所有主要的平台均可通过 Web 浏览器来访问 Web,不同的平台可以借此进行交互.为了让这些平台协同工作,Web 应用程序被开发了出来. Web 应用程序是运行在 Web ...

  10. realsense数据分析

    line: (434,300) (453,144) (0,0,0),(-0.926698,-1.25853,2.032) 0.781452 ------------------------------ ...