File size: 1,909 Bytes
			
			| f7ba5f2 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <iostream>
#include <vector>
using namespace std;
class KMP {
  const vector<int> &needle;
  vector<int> table;
 public:
  KMP(const vector<int> &needle) : needle(needle), table(needle.size()) {
    for (int i = 1, j = 0; i < (int)needle.size(); i++) {
      while (j > 0 && needle[i] != needle[j]) {
        j = table[j - 1];
      }
      if (needle[i] == needle[j]) {
        j++;
      }
      table[i] = j;
    }
  }
  int find_in(const vector<int> &haystack) {
    int m = needle.size();
    if (m == 0) {
      return 0;
    }
    for (int i = 0, j = 0; i < (int)haystack.size(); i++) {
      while (j > 0 && needle[j] != haystack[i]) {
        j = table[j - 1];
      }
      if (needle[j] == haystack[i]) {
        j++;
      }
      if (j == m) {
        return i + 1 - m;
      }
    }
    return -1;
  }
};
int N, K;
vector<int> A, B;
string solve() {
  bool is_equal = A == B;
  // Check special cases.
  if (K == 0) {
    return is_equal ? "YES" : "NO";
  }
  if (N == 2) {
    if (A[0] == A[1]) {
      return "YES"; // Same numbers.
    }
    // Different numbers.
    if (is_equal) {
      return K % 2 == 0 ? "YES" : "NO";
    }
    return K % 2 == 0 ? "NO" : "YES";
  }
  // Check array is rotated by searching for B within (A + A).
  vector<int> AA(A.begin(), A.end());
  AA.insert(AA.end(), A.begin(), A.end());
  if (K == 1 && is_equal) {
    // If K = 1 and A = B, we can't match at index 0 or N-1 of (A + A),
    // since cuts of 0 or N cards are not allowed.
    AA.erase(AA.begin());
    AA.pop_back();
  }
  return KMP(B).find_in(AA) >= 0 ? "YES" : "NO";
}
int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cin >> N >> K;
    A.resize(N);
    B.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> A[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> B[i];
    }
    cout << "Case #" << t << ": " << solve() << endl;
  }
  return 0;
}
 | 
