Advertisement
abdukodir

Общая подстрока

Feb 4th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<string.h>
  7. #include<algorithm>
  8.  
  9. #define pb push_back
  10. #define mp make_pair
  11. #define fr first
  12. #define se second
  13. #define sc scanf
  14. #define pr printf
  15.  
  16. const double eps = 1e-12;
  17. const int zero1 = 256;
  18. const int MN = 50010;
  19. const int N = 8000;
  20. const int inf = 1000010;
  21.  
  22. using namespace std;
  23. int n = 0, k, ls1, ls2;
  24. int p[MN], c[20][MN], ps2[20][MN], lps2[20];
  25. char s1[MN], s2[MN], s[MN];
  26. void Init(){
  27. sc("%s", s1);
  28. sc("%s", s2);
  29. sc("%d", &k);
  30. ls1 = strlen(s1);
  31. ls2 = strlen(s2);
  32. for(int i=0; i<N; i++){
  33. s[n++] = '#';
  34. }
  35. for(int i=0; i<ls1; i++){
  36. s[n++] = s1[i];
  37. }
  38. for(int i=0; i<N; i++){
  39. s[n++] = '#';
  40. }
  41. for(int i=0; i<ls2; i++){
  42. s[n++] = s2[i];
  43. }
  44. //pr("%s\n", s);
  45. //pr("%d %d\n", ls1, ls2);
  46. }
  47. int LCP(int i, int j, int h){
  48. int cur = 0;
  49. for(; h>=0; h--){
  50. if(c[h][i] == c[h][j] && i+(1<<h)<=N+ls1 && j+(1<<h)<=N+ls1){
  51. cur += (1<<h);
  52. i += (1<<h);
  53. j += (1<<h);
  54. }
  55. }
  56. return cur;
  57. }
  58. int find(int i, int ln){
  59. int h = 0;
  60. while((1<<h)<=ln)h++;
  61. h--;
  62. pair<int, int>a, b;
  63. int L = 0, M, j, R, H;
  64. if((1<<h) == ln){
  65. H = h;
  66. }
  67. else {
  68. H = h+1;
  69. }
  70. R = lps2[H]-1;
  71. if(R == -1)return 0;
  72. while(L<R){
  73. M = (L+R)/2;
  74. j = ps2[H][M];
  75. a = mp(c[h][i], c[h][ i+ln-(1<<h) ]);
  76. b = mp(c[h][j], c[h][ j+ln-(1<<h) ]);
  77. if(a>b){
  78. L = M+1;
  79. }
  80. else {
  81. R = M;
  82. }
  83. }
  84. j = ps2[H][R];
  85. a = mp(c[h][i], c[h][ i+ln-(1<<h) ]);
  86. b = mp(c[h][j], c[h][ j+ln-(1<<h) ]);
  87. return (a == b);
  88. }
  89. int classes, cnt[MN], pn[MN];
  90. void solve(){
  91. memset(cnt, 0, zero1*sizeof(int));
  92. for(int i=0; i<n; i++){
  93. cnt[ s[i] ]++;
  94. }
  95. for(int i=1; i<zero1; i++){
  96. cnt[i] += cnt[i-1];
  97. }
  98. for(int i=0; i<n; i++){
  99. p[ --cnt[ s[i] ] ] = i;
  100. }
  101. for(int i=0; i<n; i++){
  102. if(p[i]>=N+N+ls1){
  103. ps2[0][ lps2[0]++ ] = p[i];
  104. }
  105. }
  106. c[0][ p[0] ] = 0;
  107. classes = 1;
  108. for(int i=1; i<n; i++){
  109. if(s[ p[i] ] != s[ p[i-1] ]){
  110. classes++;
  111. }
  112. c[0][ p[i] ] = classes-1;
  113. }
  114.  
  115.  
  116. for(int h=0; (1<<h)<ls1; h++){
  117. for(int i=0; i<n; i++){
  118. pn[i] = p[i]-(1<<h);
  119. if(pn[i]<0){
  120. pn[i] += n;
  121. }
  122. }
  123. memset(cnt, 0, classes*sizeof(int));
  124. for(int i=0; i<n; i++){
  125. cnt[ c[h][ pn[i] ] ]++;
  126. }
  127. for(int i=1; i<classes; i++){
  128. cnt[i] += cnt[i-1];
  129. }
  130. for(int i=n-1; i>=0; i--){
  131. p[ --cnt[ c[h][ pn[i] ] ] ] = pn[i];
  132. }
  133. for(int i=0; i<n; i++){
  134. if(p[i]>=N+N+ls1){
  135. ps2[h+1][ lps2[h+1]++ ] = p[i];
  136. }
  137. }
  138. c[h+1][ p[0] ] = 0;
  139. classes = 1;
  140. for(int i=1; i<n; i++){
  141. int min1 = (p[i]+(1<<h))%n;
  142. int min2 = (p[i-1]+(1<<h))%n;
  143. if(c[h][ p[i] ] != c[h][ p[i-1] ] || c[h][ min1 ] != c[h][ min2 ]){
  144. classes++;
  145. }
  146. c[h+1][ p[i] ] = classes-1;
  147. }
  148. }
  149. int q = 0;
  150. for(int i=0; i<n; i++){
  151. if(p[i]>=N && p[i]<N+ls1){
  152. p[q++] = p[i];
  153. }
  154. }
  155. /*
  156. for(int i=0; i<q; i++){
  157. pr("%d ", p[i]);
  158. }
  159. pr("\n");
  160. */
  161. int h = 0, lcp[MN] = {0};
  162. while((1<<h)<=ls1)h++;
  163. h--;
  164. for(int i=1; i<q; i++){
  165. lcp[i] = LCP(p[i], p[i-1], h);
  166. //pr("%d %d %d\n", p[i], p[i-1], lcp[i]);
  167. }
  168. for(int i=0; i<q; i++){
  169. for(int j=lcp[i]+1; j+p[i]<=N+ls1; j++){
  170. k -= find(p[i], j);
  171. if(k == 0){
  172. for(k=0; k<j; k++){
  173. pr("%c", s[ p[i]+k ]);
  174. }
  175. pr("\n");
  176. exit(0);
  177. }
  178. }
  179. }
  180. }
  181. main(){
  182. freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  183. //freopen("paint.in", "r", stdin); freopen("paint.out", "w", stdout);
  184. Init();
  185. solve();
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement