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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

string matching(HDU-6629)

發(fā)布時(shí)間:2025/3/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 string matching(HDU-6629) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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 組數(shù)據(jù),每組給出一個字符串 S,現(xiàn)在有一個函數(shù),每次會暴力的跑?S 的所有后綴與字符串 S 的最長公共前綴,問這個函數(shù)會運(yùn)行多少次

思路:

實(shí)質(zhì)是求?S 的每個后綴與 S 的最長公共前綴的比較次數(shù)之和

擴(kuò)展 KMP 模版題,求出 extend 數(shù)組后比較累加即可

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

?

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 七仙女欲春2一级裸体片 | 中文字幕乱妇无码av在线 | 天堂男人av| 亚洲视频免费播放 | 欧美日韩高清一区二区三区 | 日韩av中文字幕在线免费观看 | 亚洲国产中文在线 | 欧美爱爱免费视频 | 在线播放一区 | 四虎影院免费 | 久久久精品影院 | 干爹你真棒插曲mv在线观看 | 吻胸摸激情床激烈视频 | 国产免费一级视频 | 超碰在97| 男生女生搞鸡视频 | 久久女女 | 久久久观看| 超污巨黄的小短文 | 少妇综合网 | 在线观看免费成人 | 久久免费视频网站 | 国产suv精品一区二区三区 | 中文视频在线观看 | 精品欧美在线观看 | 国产在线一二区 | 岛国av一区二区三区 | 久久久午夜精品福利内容 | 99re这里都是精品 | 日批视频免费 | gogo人体做爰大胆视频 | 国产一区视频在线观看免费 | 欧美一区二区在线 | 欧洲在线一区 | a网站在线 | 精品成人无码久久久久久 | 火影黄动漫免费网站 | 亚欧美精品 | 日本成人在线播放 | 男人午夜视频 | 欧美7777 | 久久99中文字幕 | 欧美在线色视频 | 91二区 | 亚洲AV无码成人国产精品色 | 欧美浓毛大泬视频 | 中文字幕日韩一区二区 | 亚洲大片免费 | 少妇av片 | 日韩精品在线观看AV | 制服诱惑一区 | 青草超碰 | 国产一区二区三区小说 | 日本高清网站 | 中文字幕 自拍偷拍 | 欧美黄色成人 | 草草影院一区二区三区 | 超碰网站在线观看 | 老司机性视频 | 久久久久国产视频 | 一级黄色免费毛片 | av在线第一页 | 91视频合集| 中文字幕一区二区三区免费 | 国产精品国产三级国产播12软件 | 激情专区| 久久久久亚洲AV成人无在 | 俄罗斯乱妇 | 日本久久久久久久久久 | 日韩免费观看 | 欧美精品三区 | 国产精品资源在线 | 成人免费高清在线播放 | 亚洲码视频 | 国产成人超碰人人澡人人澡 | 日本欧美国产在线 | 日韩精品一区在线观看 | 精品福利在线视频 | 18视频在线观看男男 | 亚洲精品视频一区二区三区 | 深夜久久| 性感美女视频一二三 | 久久久久久欧美精品se一二三四 | 日韩毛片高清在线播放 | 亚色图| 91视频毛片 | 伦理欧美| 国产亚洲精品久久 | 国产乱子伦农村叉叉叉 | 黄瓜视频色版 | 亚洲精品一区二区三区四区 | 爱情岛亚洲论坛入口 | 国产偷自拍 | 免费在线观看av片 | 黄色成年视频 | 成人依依网 | 九九热伊人 | 日韩av在线一区 | 男人猛吃奶女人爽视频 |