[Gym101981M] [2018ICPC Nanjing M title] Mediocre String Problem

Topic Link

Title effect is asked to find the $ S $ string interval $ [i, j] $, to find the position of $ k $ in $ T $ string, so that $ S [i, j] $ and $ T [1, k] $ palindromic sequence can be composed, and $ j-i + 1> k $, seeking such triples $ (i, j, k) $ number.

A start bit ignorant, but think carefully, because $ j-i + 1> k $, so $ S [i, j] $ contains a certain part of the latter half of a palindromic sequence, i.e., $ S [i, j ] $ suffix is ​​a certain palindromic sequence.

If the string is a palindrome $ S [x, j] $, the remaining $ S [i, x-1] $ and $ T [1, k] $ string you should be able to form a palindrome. If the string S $ $ inverted, the string $ S ^ { '} on the original $ $ S [i, x-1] $ position $ T [1, k] $ should be the same.

It should be relatively straightforward problem-solving mode, the string S $ $ inverted, and then seek the extended $ $ KMP, to give the string $ S ^ { '} $ longest common prefix and suffix of each string of $ T $. Then the string $ S ^ { '} $ palindromic automaton constructed.

Can be obtained string $ S ^ { '} $ number palindromic sequence at each position as the end of the palindromic substring. Then enumeration string $ S ^ { '} $ each position $ i $, to the current position as in the above-$ x $, and then calculate the contribution of the current position of the answer.

  . 1 #include <bits / STDC ++ H.>
   2  the using  namespace STD;
   . 3 typedef Long  Long LL;
   . 4  const  int MAXN = 1e6 + 100 ;
   . 5  int the Next [MAXN];
   . 6  int Ex [MAXN];
   . 7  void for getN ( char * S1) { // Qiuzi string matches itself 
  . 8      int I = 0 , J, P, len = strlen (S1);
   . 9      the Next [ 0 ] = len;
 10      the while (I + . 1 < len && s1[i] == s1[i + 1])
 11         i++;
 12     Next[1] = i;
 13     p = 1;
 14     for (i = 2; i < len; i++) {
 15         if (Next[i - p] + i < Next[p] + p)
 16             Next[i] = Next[i - p];
 17         else {
 18             j = Next[p] + p - i;
 19             if (j < 0)
 20                 j = 0;
 21             while (i + j < len && s1[j] == s1[i + j])
 22                 j++;
 23             Next[i] = j;
 24             p = i;
 25         }
 26     }
 27 }
 28 void getE(char* s1, char* s2) {//求子串与主串匹配
 29     int i = 0, j, p, len1 = strlen(s1), len2 = strlen(s2);
 30     while (i < len1 && i < len2 && s1[i] == s2[i])
 31         i++;
 32     Ex[0] = i;
 33     p = 0;
 34     for (i = 1; i < len1; i++) {
 35         if (Next[i - p] + i < Ex[p] + p)
 36             Ex[i] = Next[i - p];
 37         else {
 38             j = Ex[p] + p - i;
 39             if (j < 0)
 40                 j = 0;
 41             while (i + j < len1 && j < len2 && s1[i + j] == s2[j])
 42                 j++;
 43 is              Ex [I] = J;
 44 is              P = I;
 45          }
 46 is      }
 47  }
 48  struct Palindromic_Tree {
 49      int Next [MAXN] [ 26 is ]; // to point to the current string ends with a character string constituting plus 
50      int fail [MAXN]; // fail pointer to jump to the fail mismatch node pointer 
51 is      int CNT [MAXN]; // represents the number of essentially different nodes i represents a string, with the final count statistics 
52      int NUM [MAXN]; // represents a palindromic sequence number of the longest string of the rightmost point of the end of the palindromic palindromic sequence represented by node i 
53 is      int len [MAXN]; //len [i] represents the node i indicates a length of a palindromic sequence 
54 is      int ID [MAXN]; // represents the subscript i in the array which the position of the automatic machine 
55      int S [MAXN];
 56 is      int Last; // point to a node character is located, conveniently once the Add 
57 is      int n-; int P;
 58      int newNode ( int X) {
 59          for ( int I = 0 ; I < 26 is ; I ++) next [P] [I] = 0 ;
 60          CNT [P] = 0 ; NUM [P] = 0 ; len [P] = X;
 61 is          return P ++ ;
62 is      }
 63 is      void the init () { // Initialization 
64          P = 0 ;
 65          newNode ( 0 ); newNode (- . 1 );
 66          Last = 0 ; n-= 0 ;
 67          S [n-] = - . 1 ;
 68          Fail [ 0 ] = . 1 ;
 69      }
 70      int get_fail ( int X) { // the longest find a mismatch 
71 is          the while (S [n-- len [X] - . 1 ] = S [n-]!) X =Fail [X];
 72          return X;
 73 is      }
 74      void the Add ( int X) {
 75          S [n-++] = X;
 76          int CUR = get_fail (Last); // Get the palindromic sequence by a palindrome string matching position 
77          IF (! Next [CUR] [X]) { // If there have been no palindromic sequence, indicate that there is a new different nature palindromic sequence 
78              int now newNode = (len [CUR] + 2 ); // Create a node 
79              ID [n-- . 1 ] = now;
 80              fail [now] Next = [get_fail (fail [CUR])] [X]; // after establishing pointers fail to jump mismatch
 81             next[cur][x] = now;
 82             num[now] = num[fail[now]] + 1;
 83         }
 84         else
 85             id[n - 1] = next[cur][x];
 86         last = next[cur][x];
 87         cnt[last]++;
 88     }
 89     void count() {
 90         for (int i = p - 1; i >= 0; --i) cnt[fail[i]] += cnt[i];
 91     }
 92 
 93 }a;
 94 char s[maxn], s1[maxn], t[maxn];
 95 int main() {
 96     scanf("%s%s", s, t);
 97     int n = strlen(s), m = strlen(t);
 98     for (int i = 0; i < n; i++)
 99         s1[i] = s[n - i - 1];
100     getN(t);
101     getE(s1, t);
102     a.init();
103     for (int i = 0; i < n; i++)
104         a.add(s1[i] - 'a');
105     a.count();
106     ll ans = 0;
107     for (int i = n - 1; i >= 0; i--) {
108         int w = Ex[i];
109         ans += 1LL * w * a.num[a.id[i - 1]];
110     }
111     printf("%lld\n", ans);
112 }

 

 

 

 

Guess you like

Origin www.cnblogs.com/sainsist/p/11622825.html