hiho 1625 重复字符串匹配 [Offer收割]编程练习赛35 Problem C KMP模板题

来源:互联网 发布:js事件兼容性写法 编辑:程序博客网 时间:2024/05/21 10:18

题目3 : 重复字符串匹配

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

给定两个字符串A和B,请你求出字符串A最少重复几次才能使得B是A的子串。  

例如A="hiho",B="hohihohi"。则A重复3次之后变为"hihohihohiho",这时B是A的子串。

输入

输入包含多组数据。  

第一行包含一个整数T,表示数据组数。 (1 ≤ T ≤ 5)  

对于每组数据,第一行包含一个字符串A,第二行包含一个字符串B。  

对于30%的数据,1 ≤ |A|, |B| ≤ 1000  

对于100%的数据, 1 ≤ |A|, |B| ≤ 100000 并且A和B都只包含小写字母。

输出

A最少重复的次数。如果无论重复多少次也不能包含B,输出-1。

样例输入
2hiho  hohihohi  hiho  coder
样例输出
3-1
KMP模板题,先让A长度大于等于B,比较一下,不行就再加一个A,比较一下。。。。其实做一遍KMP就够了,这里为了不探讨匹配的位置,多做了一遍KMP


#include <iostream>#include <stdio.h>#include <math.h>#include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <stack>#include <bits/stdc++.h>using namespace std;int T;string A,B;/*class Solution(object):    def strStr(self, haystack, needle):        """        :type haystack: str        :type needle: str        :rtype: int        """        if needle=="":return 0        Len1=len(haystack)        Len2=len(needle)        if Len2>Len1:return -1        Next=[0]*(Len2+1)        i=0        j=0        t=-1        Next[0]=-1        while j<Len2:            if t<0 or needle[j]==needle[t]:                j+=1                t+=1                Next[j]=t            else:                t=Next[t]        i=0        j=0        while i<Len1 and j<Len2:            if j<0 or haystack[i]==needle[j]:                if j+1==Len2:return i-j                i+=1                j+=1            else:                j=Next[j]        return -1*/int Next[100009];int KMP(string s,string p){    int len1=s.length();    int len2=p.length();    memset(Next,0,sizeof(Next));    int i=0;    int j=0;    int t=-1;    Next[0]=-1;    while(j<len2){        if(t<0||p[j]==p[t]){            j+=1;            t+=1;            Next[j]=t;        }        else{            t=Next[t];        }    }    i=0;    j=0;    while(i<len1&&j<len2){        if(j<0||s[i]==p[j]){            if(j+1==len2)return i-j;            i+=1;            j+=1;        }        else{            j=Next[j];        }    }    return -1;}int main(){    cin>>T;    while(T--){        cin>>A>>B;//        cout<<A<<endl<<B<<endl;        int len1=A.length();        int len2=B.length();        int timesA=(len2-1)/len1+1;//at least        string t="";        for(int i=0;i<timesA;i++){            t+=A;        }        if(KMP(t,B)!=-1){            cout<<timesA<<endl;            continue;        }        t+=A;        if(KMP(t,B)==-1){            cout<<-1<<endl;        }        else{            cout<<timesA+1<<endl;        }    }    return 0;}////                       _oo0oo_//                      o8888888o//                      88" . "88//                      (| -_- |)//                      0\  =  /0//                    ___/`---'\___//                  .' \\|     |// './/                 / \\|||  :  |||// \//                / _||||| -:- |||||- \//               |   | \\\  -  /// |   |//               | \_|  ''\---/''  |_/ |//               \  .-\__  '-'  ___/-. ///             ___'. .'  /--.--\  `. .'___//          ."" '<  `.___\_<|>_/___.' >' "".//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |//         \  \ `_.   \_ __\ /__ _/   .-` /  ///     =====`-.____`.___ \_____/___.-`___.-'=====//                       `=---='//////     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~////               佛祖保佑         永无BUG//////



阅读全文
1 0
原创粉丝点击