Advertisement
Guest User

Untitled

a guest
Apr 15th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int n;
  6. int A[1000005], next[1000005] = {-1};
  7. int B[2000010];
  8.  
  9. int main()
  10. {
  11.  
  12.     int pos = -1;
  13.  
  14.     scanf("%d", &n);
  15.     for (int i=0; i<n; i++) scanf("%d", &A[i]);
  16.     for (int i=0; i<n; i++) scanf("%d", &B[i]);
  17.  
  18.     memcpy(B + n, B, sizeof(int)*n);
  19.  
  20.     for (int i = 0, j = -1; i < n; ){
  21.         while (j > -1 && A[i] != A[j]) j = next[j];
  22.         i++;
  23.         j++;
  24.         next[i] = j;
  25.     }
  26.  
  27.     for (int i = 0, j = 0; i < 2*n; ){
  28.         while (j > -1 && B[i] != A[j]) j = next[j];
  29.         i++;
  30.         j++;
  31.         if (j == n){
  32.             pos = 2*n - i;
  33.             j = next[j];
  34.         }
  35.     }
  36.  
  37.     if (pos >= 0) printf("DA\n%d\n", pos);
  38.     else printf("NE\n");
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement