亲串 (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个小朋友,每个小朋友手里有一些糖块, ...
随机推荐
- layer iframe 之间传值和关闭iframe弹窗
1.访问父页面元素值 var parentId=parent.$("#id").val();//访问父页面元素值 2.访问父页面方法 var parentMethodValue=p ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- gerrit docker
修改项目配置文件 git clone ssh://admin@localhost:29418/All-Projects && scp -p -P 29418 admin@localho ...
- ios开发网络学习六:设置队列请求与RunLoop
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- iis MP4 不能访问404
为什么我上传了flv或MP4文件到服务器,可输入正确地址通过http协议来访问总是出现“无法找到该页”的404错误呢?这就表明mp4格式文件是服务器无法识别的,其实,这是没有在iis中将相应的MIME ...
- ios开发抽屉效果的封装使用
#import "DragerViewController.h" #define screenW [UIScreen mainScreen].bounds.size.width @ ...
- ahks
!+F11:: newStr := clipboard ;newStr := RegExReplace(newStr, "<[^>]*>", "&quo ...
- CVE-2014-3566
https://access.redhat.com/articles/1232123 https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv ...
- 在CentOS上使用Nginx和Tomcat搭建高可用高并发网站
目录 目录 前言 创建CentOS虚拟机 安装Nginx 安装Tomcat 安装lvs和keepalived 反向代理 部署网站 搭建数据库 编写网站项目 解决session一致性 注意 参考资料 前 ...
- Xcode经常使用插件使用及自己主动生成帮助文档
*一.Xcode 插件下载:* VVDocumenter下载:https://github.com/onevcat/VVDocumenter-Xcode Xcode经常使用插件下载:http://pa ...