hdu 2203 亲和串

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2203

亲和串

Description

人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。

Input

本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。

Output

如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。

SampleInput

AABCD

CDAA

ASD

ASDF

SampleOutput

yes

no

kmp简单题,先预处理一下,然后再搞。。

 1 #include<algorithm>

 2 #include<iostream>

 3 #include<cstdlib>

 4 #include<cstring>

 5 #include<cstdio>

 6 #include<vector>

 7 #include<map>

 8 #include<set>

 9 using std::cin;

10 using std::cout;

11 using std::endl;

12 using std::find;

13 using std::sort;

14 using std::set;

15 using std::map;

16 using std::pair;

17 using std::vector;

18 using std::multiset;

19 using std::multimap;

20 #define sz(c) (int)(c).size()

21 #define all(c) (c).begin(), (c).end()

22 #define iter(c) decltype((c).begin())

23 #define cls(arr,val) memset(arr,val,sizeof(arr))

24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())

25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)

26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)

27 #define pb(e) push_back(e)

28 #define mp(a, b) make_pair(a, b)

29 const int Max_N = 100010;

30 typedef unsigned long long ull;

31 char str[Max_N];

32 struct KMP {

33     int i, j, n, m, next[Max_N];

34     char text[Max_N << 1], pat[Max_N];

35     inline void init() {

36         strcpy(text, str);

37         strcat(text, str);

38     }

39     inline void get_next() {

40         n = strlen(pat);

41         for (i = 1, j = next[0] = 0; i < n; i++) {

42             while (j > 0 && pat[i] != pat[j]) j = next[j - 1];

43             if (pat[i] == pat[j]) j++;

44             next[i] = j;

45         }

46     }

47     inline bool kmp_match() {

48         get_next();

49         m = strlen(text);

50         for (i = j = 0; i < m; i++) {

51             while (j > 0 && text[i] != pat[j]) j = next[j - 1];

52             if (text[i] == pat[j]) j++;

53             if (j == n) return true;

54         }

55         return false;

56     }

57 }kmp;

58 int main() {

59 #ifdef LOCAL

60     freopen("in.txt", "r", stdin);

61     freopen("out.txt", "w+", stdout);

62 #endif

63     while (~scanf("%s %s", str, kmp.pat)) {

64         kmp.init();

65         puts(kmp.kmp_match() ? "yes" : "no");

66     }

67     return 0;

68 }
View Code

 

你可能感兴趣的:(HDU)