hdu3374 String Problem【最小表示法】【exKMP】
阿新 • • 發佈:2018-11-20
output field can mis sep pan 題意 easy example
String Rank
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
Input
Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.
Sample Input
abcder
aaaaaa
ababab
String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4828 Accepted Submission(s): 1949
String Rank
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
Output Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
Sample Output 1 1 6 1 1 6 1 6 1 3 2 3
Author WhereIsHeroFrom
Source HDOJ Monthly Contest – 2010.04.04
題意:
找到給定的字符串$S$的最小表示法和最大表示法。並且找到他在所有循環同構串中的出現次數。
思路:
首先肯定是要先分別找出最大表示法和最小表示法對應的起始下標。
方法就是先復制一倍接在後面,然後用兩個指針分別從0和1開始往後掃描,在第一個發現不相等的位置比較$s[i+k]$和$s[j+k]$的大小。
然後用exKMP分別對最小表示法和最大表示法求$extend$數組。
然後數一下有多少個$extend[i] = n$,這就是個數。
1 #include<iostream> 2 #include<bits/stdc++.h> 3 #include<cstdio> 4 #include<cmath> 5 //#include<cstdlib> 6 #include<cstring> 7 #include<algorithm> 8 //#include<queue> 9 #include<vector> 10 //#include<set> 11 //#include<climits> 12 //#include<map> 13 using namespace std; 14 typedef long long LL; 15 #define N 100010 16 #define pi 3.1415926535 17 #define inf 0x3f3f3f3f 18 19 const int maxn = 1e6 + 6; 20 char s[maxn * 2], t[maxn]; 21 int nxt[maxn * 2], ex_min[maxn * 2], ex_max[maxn * 2]; 22 23 void GETNEXT(char *str) 24 { 25 int i=0,j,po,len=strlen(str); 26 nxt[0]=len;//初始化next[0] 27 while(str[i]==str[i+1]&&i+1<len)//計算next[1] 28 i++; 29 nxt[1]=i; 30 po=1;//初始化po的位置 31 for(i=2;i<len;i++) 32 { 33 if(nxt[i-po]+i<nxt[po]+po)//第一種情況,可以直接得到next[i]的值 34 nxt[i]=nxt[i-po]; 35 else//第二種情況,要繼續匹配才能得到next[i]的值 36 { 37 j=nxt[po]+po-i; 38 if(j<0)j=0;//如果i>po+next[po],則要從頭開始匹配 39 while(i+j<len&&str[j]==str[j+i])//計算next[i] 40 j++; 41 nxt[i]=j; 42 po=i;//更新po的位置 43 } 44 } 45 } 46 //計算extend數組 47 void EXKMP(char *s1,char *s2, int *ex) 48 { 49 int i=0,j,po,len=strlen(s1),l2=strlen(s2); 50 GETNEXT(s2);//計算子串的next數組 51 while(s1[i]==s2[i]&&i<l2&&i<len)//計算ex[0] 52 i++; 53 ex[0]=i; 54 po=0;//初始化po的位置 55 for(i=1;i<len;i++) 56 { 57 if(nxt[i-po]+i<ex[po]+po)//第一種情況,直接可以得到ex[i]的值 58 ex[i]=nxt[i-po]; 59 else//第二種情況,要繼續匹配才能得到ex[i]的值 60 { 61 j=ex[po]+po-i; 62 if(j<0)j=0;//如果i>ex[po]+po則要從頭開始匹配 63 while(i+j<len&&j<l2&&s1[j+i]==s2[j])//計算ex[i] 64 j++; 65 ex[i]=j; 66 po=i;//更新po的位置 67 } 68 } 69 } 70 71 int main() 72 { 73 while(scanf("%s", s) != EOF){ 74 int n = strlen(s); 75 for(int i = 0; i < n; i++)s[n + i] = s[i]; 76 int i = 0, j = 1, k; 77 while(i < n && j < n){ 78 for(k = 0; k < n && s[i + k] == s[j + k]; k++); 79 if(k == n)break; 80 if(s[i + k] > s[j + k]){ 81 i = i + k + 1; 82 if(i == j)i++; 83 } 84 else{ 85 j = j + k + 1; 86 if(i == j)j++; 87 } 88 } 89 int rnk_min = min(i, j); 90 91 i = 0, j = 1; 92 while(i < n && j < n){ 93 for(k = 0; k < n && s[i + k] == s[j + k]; k++); 94 if(k == n)break; 95 if(s[i + k] > s[j + k]){ 96 j = j + k + 1; 97 if(i == j)j++; 98 } 99 else{ 100 i = i + k + 1; 101 if(i == j)i++; 102 } 103 } 104 int rnk_max = min(i, j); 105 106 int min_cnt = 0; 107 memcpy(t, s + rnk_min, n); 108 EXKMP(s, t, ex_min); 109 for(int i = 0;i < n; i++){ 110 if(ex_min[i] == n)min_cnt++; 111 } 112 int max_cnt = 0; 113 memcpy(t, s + rnk_max, n); 114 EXKMP(s, t, ex_max); 115 for(int i = 0; i < n; i++){ 116 if(ex_max[i] == n)max_cnt++; 117 } 118 printf("%d %d %d %d\n", rnk_min + 1, min_cnt, rnk_max + 1, max_cnt); 119 } 120 return 0; 121 }
hdu3374 String Problem【最小表示法】【exKMP】