日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

string matching(HDU-6629)

發布時間:2025/3/17 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }

?

總結

以上是生活随笔為你收集整理的string matching(HDU-6629)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。