标签:amp art should bnu mono problem cas comm while
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
/*
Problem : 1686 ( Oulipo ) Judge Status : Accepted
RunId : 14384097 Language : G++ Author : 24862486
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 10000 + 5;
const int MAXM = 1000000 + 5;
char W[MAXN], T[MAXM];
int n, nexts[MAXN];
void Get_Next(int M) {
int i = 0,j = -1;
nexts[0] = -1;
while(i < M) {
if(j == -1 || W[i] == W[j]) {
i ++;
j ++;
if(W[i] == W[j]) {
nexts[i] = nexts[j];
} else {
nexts[i] = j;
}
} else {
j = nexts[j];
}
}
}
int Get_KMP(int N,int M) {
int i = 0,j = 0 ,num = 0;
while(i < N) {
if(j == -1 || W[j] == T[i]) {
i ++;
j ++;
} else {
j = nexts[j];
}
if(j == M) {
num ++;
j = nexts[j];//直接从此处匹配模式串的地方进行匹配。防止超时
}
}
return num;
}
int main() {
scanf("%d", &n);
while(n --) {
scanf("%s%s", W, T);
int t1 = strlen(W), t2 = strlen(T);
Get_Next(t1);
printf("%d\n", Get_KMP(t2, t1));
}
return 0;
}
标签:amp art should bnu mono problem cas comm while
原文地址:http://www.cnblogs.com/zhchoutai/p/6792446.html