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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Food Buying(CF-1296B
- 下一篇: 地图的四着色 (CSU-1508)