亲串 (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个小朋友,每个小朋友手里有一些糖块, ...
随机推荐
- [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid
For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...
- [Javascript Natural] Break up language strings into parts using Natural
A part of Natural Language Processing (NLP) is processing text by “tokenizing” language strings. Thi ...
- Java文档上传问题设计
近期公司让做一个文档上传的功能,功能描写叙述大概是这样子滴 书籍名称.书籍定价.书籍封面图片(须要上传).文档内容 (须要上传) .还有其它相关的描写叙述信息. 我的设计 表 A 包括以上字段 , ...
- matplotlib学习之颜色样式
一.颜色 1.内建八种默认颜色 蓝色 - 'b' 绿色 - 'g' 红色 - 'r' 青色 - 'c' 品红 - 'm' 黄色 - 'y' 黑色 - 'k' 白色 - 'w' 2.灰度 plt.plo ...
- mysql 按日期分组
select DATE_FORMAT(NOW(),'%Y%m%d') days,count(caseid) count from tc_case group by days; //date_forma ...
- php标准库中的优先队列SplPriorityQueue怎么使用?(继承)
php标准库中的优先队列SplPriorityQueue怎么使用?(继承) 一.总结 1.new对象,然后通过insert方法和extract方法来使用,top方法也很常用. 2.类的话首先想到继承, ...
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- [NPM] Run npm scripts when files change with onchange
In this lesson we will look at how we can setup our npm scripts to execute when the file system has ...
- 小强的HTML5移动开发之路(38)——jqMobi插件ActionSheet
现在在手机客户端上Action Sheet非常常见,比如微信中的分享按钮菜单,下面我们使用jqMobi实现一个Action Sheet,如下: 首先右击上面的按钮选择审查元素(我用的是Chrome浏览 ...
- 基于Linux应用层的6LOWPAN物联网网关及实现方法
本发明涉及一种基于Linux应用层的6LOWPAN物联网网关及实现方法,所述物联网网关包括开发平台以及无线射频模块,其实现方法是:所述6LOWPAN物联网网关的以太网网口收到访问6LOWPAN无线传感 ...