标签:nat out tor group format over string alt character
InputThe first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A‘, ‘B‘, ‘C‘, …, ‘Z‘}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A‘, ‘B‘, ‘C‘, …, ‘Z‘}, with |W| ≤ |T| ≤ 1,000,000.
OutputFor every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; char W[10010],T[1000010]; int wlen,tlen; int next[10010]; void getNext() { int j,k; j=0; k=-1; next[0]=-1; while(j<wlen) { if(k==-1||W[j]==W[k]) { next[++j]=++k; } else k=next[k]; } } int KMP_count() { int ans=0; int i,j=0; if(wlen==1&&tlen==1) { if(W[0]==T[0])return 1; else return 0; } getNext(); for(i=0;i<tlen;i++) { while(j>0&&T[i]!=W[j]) j=next[j]; if(W[j]==T[i])j++; if(j==wlen) { ans++; j=next[j]; } } return ans; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%s%s",&W,&T); wlen=strlen(W); tlen=strlen(T); printf("%d\n",KMP_count()); } return 0; }
标签:nat out tor group format over string alt character
原文地址:http://www.cnblogs.com/Roni-i/p/7499079.html