string matching(HDU-6629)

Problem Description

String matching is a common type of problem in computer science. One string matching problem is as following:

Given a string s[0…len−1], please calculate the length of the longest common prefix of s[i…len−1] and s[0…len−1] for each i>0.

I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.

Input

The first line contains an integer T, denoting the number of test cases.
Each test case contains one string in a line consisting of printable ASCII characters except space.

* 1≤T≤30

* string length ≤106 for every string

Output

For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.

Sample Input

3
_Happy_New_Year_
ywwyww
zjczzzjczjczzzjc

Sample Output

17
7
32

题意:t 组数据,每组给出一个字符串 S,现在有一个函数,每次会暴力的跑 S 的所有后缀与字符串 S 的最长公共前缀,问这个函数会运行多少次

思路:

实质是求 S 的每个后缀与 S 的最长公共前缀的比较次数之和

扩展 KMP 模版题,求出 extend 数组后比较累加即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int Next[N];
int extend[N];
void getNext(char *str) {
    int len = strlen(str);
    Next[0] = len;
    int i = 0;
    while (str[i] == str[i + 1] && i + 1 < len)
        i++;
    Next[1] = i;

    int pos = 1;
    for (i = 2; i < len; i++) {
        if (Next[i - pos] + i < Next[pos] + pos)
            Next[i] = Next[i - pos];
        else {
            int j = Next[pos] + pos - i;
            if (j < 0)
                j = 0;
            while (i + j < len && str[j] == str[j + i])
                j++;
            Next[i] = j;
            pos = i;
        }
    }
}
void exKMP(char *s1, char *s2) {
    getNext(s2);
    int len1 = strlen(s1);
    int len2 = strlen(s2);

    int i = 0;
    while (s1[i] == s2[i] && i < len2 && i < len1)
        i++;
    extend[0] = i;

    int pos = 0;
    for (i = 1; i < len1; i++) {
        if (Next[i - pos] + i < extend[pos] + pos)
            extend[i] = Next[i - pos];
        else {
            int j = extend[pos] + pos - i;
            if (j < 0)
                j = 0;
            while (i + j < len1 && j < len2 && s1[j + i] == s2[j])
                j++;
            extend[i] = j;
            pos = i;
        }
    }
}

char s[N];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(Next,0,sizeof(Next));
        memset(extend,0,sizeof(extend));

        scanf("%s",s);
        exKMP(s,s);
        
        LL res=0;
        int len=strlen(s);
        for(int i=1;i<len;i++){
            res+=extend[i]+1;
            if(extend[i]+i>=len)
                res--;
        }
        printf("%lld\n",res);
    }
    return 0;
}
发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/102387089