亲串

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8049    Accepted Submission(s): 3719

Problem Description
人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,相同的问题Eddy也一直在思考,由于他在非常小的时候就知道亲和串怎样推断了。可是发现,如今长大了却不知道怎么去推断亲和串了,于是他仅仅好又再一次来请教聪明且乐于助人的你来解决问题。

亲和串的定义是这种:给定两个字符串s1和s2,假设能通过s1循环移位,使s2包括在s1中,那么我们就说s2 是s1的亲和串。

 
Input
本题有多组測试数据。每组数据的第一行包括输入字符串s1,第二行包括输入字符串s2。s1与s2的长度均小于100000。
 
Output
假设s2是s1的亲和串。则输出"yes",反之。输出"no"。每组測试的输出占一行。
 
Sample Input
AABCD
CDAA
ASD
ASDF
 
Sample Output
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)的更多相关文章

  1. HDU 2203 kmp

    http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  2. 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, ...

  3. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  4. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  5. SDUT 2772 数据结构实验之串一:KMP简单应用

    数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...

  6. SDUT OJ 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  7. SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解

    数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  9. SDUT-3331_数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个小朋友,每个小朋友手里有一些糖块, ...

随机推荐

  1. 2020发布 .NET 5 下一代全平台 .Net 框架

    [翻译] 正式宣布 .NET 5 2019-05-07 01:18 by Rwing, 16515 阅读, 79 评论, 收藏, 编辑 原文: Introducing .NET 5 今天,我们宣布 . ...

  2. 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

    1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...

  3. How to use ftp in a shell script

    转载How to use ftp in a shell script How to use ftp in a shell script Bruce EdigerBruce Ediger's home ...

  4. CentOS 7开放端口和关闭防火墙

    开放端口 永久的开放需要的端口 sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent sudo firewall-cmd -- ...

  5. 上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的,

    上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的, Html部分 <input type="file& ...

  6. 【机器学习实战】第3章 决策树(Decision Tree)

    第3章 决策树 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/ ...

  7. tig install ncursesw

    ./configure --prefix=/home/xxx/.local/ --with-ncursesw make[xxx$ ldd src/tig linux-vdso.so.1 => ( ...

  8. Mybatis中sql语句中的in查询,一定要判断null的情况

    不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...

  9. springmvc中controller内方法跳转forward?redirect?

    使用springmvc的controller的时候,碰到controller内方法的跳转的问题,记录下问题以及自己测试的过程. 场景: 业务执行更新操作之后返回列表页面,列表页面需默认展示查询的列表数 ...

  10. NOIP 模拟 序列操作 - 无旋treap

    题意: 一开始有n个非负整数h[i],接下来会进行m次操作,第i次会给出一个数c[i],要求选出c[i]个大于0的数并将它们-1,问最多可以进行多少次? 分析: 首先一个显然的贪心就是每次都将最大的c ...