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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <cstdio> #include <algorithm> #include <cstring> #include <cctype> #include <tuple> #include <queue> using namespace std; const int N = 4e3+5; struct Sam { int tot = 1, last = 1, ch[N][26], len[N], fa[N]; void insert(int c) { int cur = ++tot, pre = last; last = tot; len[cur] = len[pre]+1; while(pre&&!ch[pre][c]) ch[pre][c] = cur, pre = fa[pre]; if(!pre) return void(fa[cur] = 1); int x = ch[pre][c]; if(len[x] == len[pre]+1) return void(fa[cur] = x); int y = ++tot; fa[y] = fa[x]; fa[cur] = fa[x] = y; len[y] = len[pre]+1; memcpy(ch[y], ch[x], sizeof(ch[x])); while(pre&&ch[pre][c] == x) ch[pre][c] = y, pre = fa[pre]; } }sama, samb; struct Sqam { int root, tot, last[26], pre[N], ch[N][26]; Sqam() { root = tot = 1; for(int i = 0; i < 26; ++i) last[i] = 1; } void insert(int c) { int cur = ++tot; pre[cur] = last[c]; for(int i = 0; i < 26; ++i) for(int k = last[i]; k&&!ch[k][c]; k = pre[k]) ch[k][c] = cur; last[c] = cur; } }sqama, sqamb; char s[N]; int d[N][N]; int bfs1() { queue<pair<int, int> > q; memset(d, 0, sizeof(d)); d[1][1] = 1; q.push(make_pair(1, 1)); while(q.size()) { int xa = q.front().first, xb = q.front().second; q.pop(); for(int i = 0; i < 26; ++i) { int ya = sama.ch[xa][i], yb = samb.ch[xb][i]; if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb)); if(ya&&!yb) return d[xa][xb]; } } return -1; } int bfs2() { queue<pair<int, int> > q; memset(d, 0, sizeof(d)); d[1][1] = 1; q.push(make_pair(1, 1)); while(q.size()) { int xa = q.front().first, xb = q.front().second; q.pop(); for(int i = 0; i < 26; ++i) { int ya = sama.ch[xa][i], yb = sqamb.ch[xb][i]; if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb)); if(ya&&!yb) return d[xa][xb]; } } return -1; } int bfs3() { queue<pair<int, int> > q; memset(d, 0, sizeof(d)); d[1][1] = 1; q.push(make_pair(1, 1)); while(q.size()) { int xa = q.front().first, xb = q.front().second; q.pop(); for(int i = 0; i < 26; ++i) { int ya = sqama.ch[xa][i], yb = samb.ch[xb][i]; if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb)); if(ya&&!yb) return d[xa][xb]; } } return -1; } int bfs4() { queue<pair<int, int> > q; memset(d, 0, sizeof(d)); d[1][1] = 1; q.push(make_pair(1, 1)); while(q.size()) { int xa = q.front().first, xb = q.front().second; q.pop(); for(int i = 0; i < 26; ++i) { int ya = sqama.ch[xa][i], yb = sqamb.ch[xb][i]; if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb)); if(ya&&!yb) return d[xa][xb]; } } return -1; } int main() { scanf("%s", s); for(int i = 0; s[i]; ++i) sama.insert(s[i]-'a'), sqama.insert(s[i]-'a'); scanf("%s", s); for(int i = 0; s[i]; ++i) samb.insert(s[i]-'a'), sqamb.insert(s[i]-'a'); printf("%d\n%d\n%d\n%d\n", bfs1(), bfs2(), bfs3(), bfs4()); return 0; }
|