http://acm.nyist.net/JudgeOnline/problem.php?pid=327
亲和串
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了。于是zyc希望大家能帮他想一个办法来快速判断亲和串。亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串。
-
输入
- 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。 输出
- 如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。 样例输入
-
AABCD CDAA ASD ASDF
样例输出
-
yes no
用c++的find超时,用c语言的strstr字符串查找函数,或者BF解都行。
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int const MAX = 200000 + 10; int main() { char ch[MAX], ck[MAX]; while (scanf ("%s%s", ch, ck) != EOF) { int len = strlen(ch); for (int i = 0; i < len; i++) ch[i + len] = ch[i]; ch[2 * len] = '\0'; if (strstr(ch, ck) != 0) cout << "yes" << endl;//查找字符串的函数 else cout << "no" << endl; } }
#include<stdio.h> #include<cstring> #define MAX 100010 char s1[MAX], s2[MAX], s3[2 * MAX]; int main() { int n1, n2, n3, f, i, j; while(scanf ("%s%s", s1, s2) != EOF) { strcpy(s3, s1); //把s1拷贝到s3上面。 strcat(s3, s1); //再次把s1接到s3上面 n1 = strlen(s1); n2 = strlen(s2); n3 = strlen(s3); if (n1 < n2) printf ("no\n"); else { j = 0; for (i = 0; i < n2 && j < n3; i++) { //BF求解 if (s3[j++] != s2[i]) { i = 0; f = 1; continue; } else f = 0; } if (f == 0) printf ("yes\n"); else printf ("no\n"); } } }