-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1282_Computer_Game.cpp
More file actions
65 lines (58 loc) · 1.38 KB
/
Copy path1282_Computer_Game.cpp
File metadata and controls
65 lines (58 loc) · 1.38 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
// Problem#: 1282
// Submission#: 1543963
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int next[100000];
int p[100000];
int c[1000000];
int n,m;
void Next(){
next[0] = -1;
int k = -1;
for(int i = 1;i < n;i ++){
while(k != -1 && p[i] != p[k + 1]){
k = next[k];
}
if(p[i] == p[k + 1]){
k ++;
}
next[i] = k;
}
}
void Match(){
int i = 0;
int k = -1;
while(i < m){
while(c[i] != p[k + 1] && k != -1){
k = next[k];
}
if(c[i] == p[k + 1]){
i ++;
k ++;
if(k == n - 1){
cout << i - n << endl;
return;
}
}
else i ++;
}
cout << "no solution" << endl;
}
int main(){
while(scanf("%d",&n) != EOF){
for(int i = 0;i < n;i ++){
scanf("%d",&p[i]);
}
scanf("%d",&m);
for(int i = 0;i < m;i ++){
scanf("%d",&c[i]);
}
Next();
Match();
}
}