亲串 (hdu 2203 KMP)
亲串
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8049 Accepted Submission(s): 3719
亲和串的定义是这种:给定两个字符串s1和s2,假设能通过s1循环移位,使s2包括在s1中,那么我们就说s2 是s1的亲和串。
AABCD
CDAA
ASD
ASDF
yes
no
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100010
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; char str[2*maxn],pstr[2*maxn];
int nextval[2*maxn]; void get_nextval()
{
int plen=strlen(pstr);
int i=0,j=-1;
nextval[0]=-1;
while (i<plen)
{
if (j==-1||pstr[i]==pstr[j])
{
i++;
j++;
if (pstr[i]!=pstr[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else
j=nextval[j];
}
} bool KMP()
{
int i=0,j=0;
int len=strlen(str);
int plen=strlen(pstr);
while (i<len&&j<plen)
{
if (j==-1||str[i]==pstr[j])
{
i++;
j++;
}
else
j=nextval[j];
}
if (j==plen)
return true;
return false;
} int main()
{
while (~scanf("%s%s",str,pstr))
{
int len=strlen(str);
for (int i=0;i<len;i++)
str[len+i]=str[i];
str[len+i]='\0';
get_nextval();
if (KMP())
printf("yes\n");
else
printf("no\n");
}
return 0;
}
/*
AABCD
CDAA
ASD
ASDF
*/
版权声明:本文博客原创文章。博客,未经同意,不得转载。
亲串 (hdu 2203 KMP)的更多相关文章
- HDU 2203 kmp
http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- HDU - 2203 KMP水题
循环移位的套路操作就是一份折开变两份 /*H E A D*/ void match(){ int n=strlen(T+1); int m=strlen(P+1); int j=0; rep(i,1, ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- SDUT 3311 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...
- SDUT 2772 数据结构实验之串一:KMP简单应用
数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...
- SDUT OJ 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解
数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- SDUT-3331_数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个小朋友,每个小朋友手里有一些糖块, ...
随机推荐
- HDU 1284 钱币兑换问题 母函数、DP
题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Python 语法细节(Python 2.x 与 Python 3.x 语法差异)
Language differences and workarounds 查询 Python 语言版本: >> import sys >> sys.version '3.5.2 ...
- 【10.58%】【codeforces 721C】Journey
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Ramda] Curry, Compose and Pipe examples
const curry = R.curry((fns, ary) => R.ap(fns, ary)); ), R.add()]); ,,]); console.log(res); //[2, ...
- http500:服务器内部错误案例详解(服务器代码语法错误或者逻辑错误)
http500:服务器内部错误案例详解(服务器代码语法错误或者逻辑错误) 一.总结 服务器内部错误可能是服务器中代码运行的时候的语法错误或者逻辑错误 二.http500:服务器内部错误案例详解 只是一 ...
- 标准模板库(STL) map —— 初始化问题
map 容器没有:.reverse成员: map 是关联式容器,会根据元素的键值自动排序: map 容器不是连续的线性空间: 标准 STL 使用 RB-tree 为底层机制 ⇒ 自动排序(关于键值): ...
- QQ欢乐斗地主心得体会 (三):高倍场攻略
上个月,也可能是上个月的上个月,我的欢乐豆兵积攒到了百万之众,突破了历史记录.最近2个月一直在高倍场混,想写点高倍场的心得体会.本篇的体会,与以前写的2篇斗地主心得体会,有点不同. 特别说明:我主要玩 ...
- android获取和展示音乐的频谱
做了个音乐播放器 就一直想做个加一个音乐频谱的展示界面 觉的这是一个好玩的东西,可以将耳边动听的声音形象化,仿佛眼前可以看到声音一样. 但是我在文档的开发者指南里没有讲任何有关音乐频谱的东西,最后还是 ...
- 要求两个异步任务都完成后, 才能回到主线程:dispatch_group_t
需求:两个异步任务都完成后, 回到主线程 /** 1.下载图片1和图片2 2.将图片1和图片2合并成一张图片后显示到imageView上 思考: * 下载图片 : 子线程 * 等2张图片都下载完毕后, ...
- HDU 1244 Max Sum Plus Plus Plus - dp
传送门 题目大意: 给一个序列,要求将序列分成m段,从左至右每一段分别长l1,l2,...lm,求最大的和是多少. 题目分析: 和最大m段子段和相似,先枚举\(i \in [1,m]\),然后$j \ ...