kmp跑两串的最大相同前后缀 HDU2594
题意: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的更多相关文章
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 2594(求最长公共前后缀 kmp)
题意是在所给的两个字符串中找最长的公共前后缀,即第一个字符串前缀和第二个字符串后缀的最长相等串. 思路是将两个字符串拼接在一起,然后直接套用 kmp 算法即可. 要注意用 next 会报编译错误,改成 ...
- 利用KMP算法解决串的模式匹配问题(c++) -- 数据结构
题目: 7-1 串的模式匹配 (30 分) 给定一个主串S(长度<=10^6)和一个模式T(长度<=10^5),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串 ...
- Objective-C 【NSString-字符串比较&前后缀检查及搜索】
———————————————————————————————————————————NSString 字符串比较 #import <Foundation/Foundation.h> vo ...
- 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 ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- 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; ...
- OKR-Periods of Words【KMP最小前后缀】
OKR-Periods of Words 传送门:链接 来源:UPC 8180 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得 ...
- Magic Potion(最大流,跑两遍网络流或者加一个中转点)
Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...
随机推荐
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- css偷懒神奇
偷懒神奇链接:https://qishaoxuan.github.io/css_tricks/glass/
- 使用Ajax向SpringMVC传递Json数据
这篇文章已经过时了. 请参考比较合适的前后端交互方式. 1.保证SpringMVC配置成功了. 2.在pom.xml中追加Jackson相关的依赖 <dependency> <gro ...
- 学python必须知道的30个技巧
收集这些有用的捷径技巧 1. 原地进行交换两个数字 我们对赋值的右侧进行一个新的元组,左侧解析(unpack)那个(未被引用的)元组到变量 <a> 和 <b> 赋值完成时,新的 ...
- mysql.zip版本的安装教程
MySQL zip版本安装 一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev. ...
- 手游折扣app票选结果公布哪个好哪个靠谱一目了然
2018年,是中国改革开放40年,也是中国互联网20年.“互联网推动了精神文明向更高水平的迈进,实现人的价值第一,创造美好生活,从生产高于生活.艺术高于成活,转向发现与实现生活本身美好,让想象成真.如 ...
- windows把zookeeper注册成服务
1.官网下载zookeeper:http://mirrors.hust.edu.cn/apache/zookeeper/. 2.修改zookeeper下面的文件/zookeeper/conf/zoo_ ...
- Linux下nginx配置https协议访问
一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...
- labelme
项目:https://github.com/wkentaro/labelme?tdsourcetag=s_pcqq_aiomsg 说明:https://www.bilibili.com/video/a ...
- Spring Security(01)——初体验
(注:本文是基于Spring Security3.1.6所写) (注:原创文章,转载请注明出处.原文地址:http://elim.iteye.com/blog/2154299) (注:本文是基于Spr ...