language: C++ 4.7.2 (gcc-4.7.2)
date: 1175 days 19 hours ago
link:
visibility: public
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
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
 
bool is_rotation(const string& str1, const string& str2)
{
  if(str1.size()!=str2.size())
    return false;
 
  vector<size_t> prefixes(str1.size(), 0);
  for(size_t i=1, j=0; i<str1.size(); i++) {
    while(j>0 && str1[i]!=str1[j])
      j=prefixes[j-1];
    if(str1[i]==str1[j]) j++;
    prefixes[i]=j;
  }
 
  size_t i=0, j=0;
  for(; i<str2.size(); i++) {
    while(j>0 && str2[i]!=str1[j])
      j=prefixes[j-1];
    if(str2[i]==str1[j]) j++;
  }
  for(i=0; i<str2.size(); i++) {
    if(j>=str1.size()) return true;
    while(j>0 && str2[i]!=str1[j])
      j=prefixes[j-1];
    if(str2[i]==str1[j]) j++;
  }
 
  return false;
}
 
int main()
{
  string s1, s2;
  while(cin >> s1 >> s2) {
    cout << is_rotation(s1, s2) << endl;
  }
  return 0;
}
 
  • upload with new input
  • result: Success     time: 0s    memory: 2812 kB     returned value: 0

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaa
    aaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    stackoverflow
    stackoverflwo
    xxxxxxx
    x
    aaaaaa
    aaaaaa
    abcde
    abcde
    1
    0
    0
    1
    1