-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle.cpp
More file actions
77 lines (61 loc) · 1.57 KB
/
Copy pathcycle.cpp
File metadata and controls
77 lines (61 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// main.cpp
// cycle
//
// Created by muchuanyun on 16/10/4.
// Copyright © 2016 muchuanyun. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100005
char s1[MAXLEN];
char s2[MAXLEN];
bool isCycle(char* s1, char* s2)
{
int len1 = (int) strlen(s1);
int len2 = (int) strlen(s2);
bool flag = false;
if ( len1 != len2)
return false;
int i = 0; // standard s1
int j = 0; // pattern s2
while ( j < len2 && i < len1) {
if (s1[i] == s2[j]) {
if (i == len1 - 1) {
int l = 0;
int k = j+1;
while (l < i - j && k < len2) {
if (s1[l++] != s2[k++]) {
flag = false;
return flag;
}
}
flag = true;
}
i++;
j++;
} else {
i = (i - j + 1);
j = 0;
}
}
return flag;
}
int main() {
#ifndef _OJ_
freopen("/Users/muchuanyun/Documents/001-Research/03-Moocs/DS_THU/dsa_pa/pa2/cycle/cycle/input.in", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
setvbuf(stdin, (char*)malloc(1 << 20), _IOFBF, 1 << 20);
setvbuf(stdout, (char*)malloc(1 << 20), _IOFBF, 1 << 20);
while (scanf("%s %s\n", s1, s2) == 2) {
//printf("%s %s\n", s1, s2);
if (isCycle(s1, s2)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}