比赛 |
20120706 |
评测结果 |
AAAAAAAAAA |
题目名称 |
解密 |
最终得分 |
100 |
用户昵称 |
CC |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-07-06 11:43:01 |
显示代码纯文本
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;
map<string,int> pos;
string st;
int n,m,ans;
int w[1000005],t[1000005],f[1000005];
void getpre() {
int j = 0;
f[1] = 0;
for (int i = 2;i <= m;i++) {
while (j && t[j + 1] != t[i]) j = f[j];
if (t[j + 1] == t[i]) j++;
f[i] = j;
}
}
int kmp() {
int j = 0,u;
for (int i = 1;i <= n;i++) {
u = w[i];
if (w[i] + j + 1 > m) u = -1;
while (j && t[j + 1] != u)
j = f[j];
if (t[j + 1] == u) j++;
if (j == m) {
return i;
}
}
return 0;
}
void solve() {
getpre();
ans = kmp();
ans -= (m - 1);
cout << ans;
}
int main() {
freopen("kriptogram.in","r",stdin);
freopen("kriptogram.out","w",stdout);
st = "";
n = 0;
while (st != "$") {
cin >> st;
n++;
w[pos[st]] = n;
pos[st] = n;
}
pos.clear();
m = 0;
st = "";
while (st != "$") {
cin >> st;
m++;
t[pos[st]] = m;
pos[st] = m;
}
m--;
n--;
for (int i = 1;i <= n;i++) {
if (!w[i]) w[i] = -1;
else w[i] -= i;
if (w[i] > m - 1) w[i] = -1;
}
for (int i = 1;i <= m;i++) {
if (!t[i]) t[i] = -1;
else t[i] -= i;
}
solve();
return 0;
}