Powerful Incantation
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1555 Accepted Submission(s): 688
Problem Description
Some dangerous Witches invaded the garden city! As a righteous magician, james0zan want to find the most powerful incantation so he can beat those dangerous witches easily.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
Input
The first line is an integer t (t<=30), the number of test cases. Each of the next t lines contains two strings, represent the incantation (length<=10^6) and the “magic index” (length<=5). Every char in the incantation and the magic index is lowercase.
Output
For each test case you should output the power of the incantation.
Sample Input
3 aaaa aa bsdjfassdiifo sd papapapapapapap ap
Sample Output
2 2 7
解题报告:
这道题就是简单的子串匹配问题,唯一特别之处是:匹配到的两个子串不能有重叠区域。做这题是练练子串匹配的算法而已:朴素字符串匹配算法、RK算法、KMP算法。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
using namespace std;
char str[1000000+5],ptn[5+5];
void compute_prefix_function(char *a,int *pi,int m);
int main()
{
int T,cnt;
cin>>T;
while(T--)
{
cnt=0;
scanf("%s%s",str,ptn);
int n=strlen(str),m=strlen(ptn);
// // 方法1:朴素字符串匹配
// int i,j;
// for(i=0;i<n;++i)
// {
// for(j=0;j<m;++j)
// {
// if( ptn[j]!=str[i+j] ) break;
// }
// if(j==m) {++cnt; i += m-1;}
// }
// // 方法2:Rabin-Karp算法
// int q = 10007,hash_value=str[0],ptn_value=ptn[0];
// for(int i=1;i<m;++i){
// ptn_value = ( (ptn_value<< 1) + (int)ptn[i] )%q;
// hash_value = ( (hash_value<< 1) + (int)str[i] )%q;
// }
//
// int j;
// int k=-m; 题意:重叠的只算一次
// for(int i=0;i<=n-m;++i){
// if(i-k<m) goto skip; 题意:重叠的只算一次
// if(hash_value == ptn_value)
// {
// for(j=0;j<m;++j)
// if(ptn[j]!=str[i+j]) break;
// if(j==m) {++cnt;k=i;}
// }
// skip: hash_value =( ( ( hash_value - (str[i]<<(m-1)) )<<1 )+str[i+m] )%q;
// }
// 方法3:KMP算法
int pi[m]; //前缀函数\pi
compute_prefix_function(ptn,pi,m);//计算前缀函数\pi
int k=-1,kk=-m;//
for(int i=0;i<n;++i)
{
while(k>-1 && str[i]!=ptn[k+1]) k=pi[k];
if( str[i]==ptn[k+1] ) {++k;}
if(k==m-1 && kk+m-1<i) {++cnt; kk=i;} 题意:重叠的只算一次
}
cout<<cnt<<endl;
}
return 0;
}
void compute_prefix_function(char *a,int *pi,int m)
{
int k=-1;
pi[0]=-1;
for(int i=1;i<m;++i){
while(k>-1 && a[i]!=a[k+1]) k=pi[k];
if(a[i]==a[k+1]) pi[i] = ++k;
else pi[i]=k;
}
}