【HDU 4150】Powerful Incantation —— 字符串匹配

14 篇文章 0 订阅
1 篇文章 0 订阅

原题链接


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.
 

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;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值