Harry and magic string HDU - palindrome string 5157 record number of disjoint

Meaning of the questions:

Recording disjoint palindromic sequence number of

answer:

The opposite of being a palindrome tree came again

Sum1 [i] with i represents the position number of the prefix string occurring and palindromic

sun2 [i] denotes the inverse number of

ans+=sum1【i-1】*sum2【i】

 

  1 #include <set>
  2 #include <map>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstring>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <unordered_map>
 14 
 15 #define  pi    acos(-1.0)
 16 #define  eps   1e-9
 17 #define  fi    first
 18 #define  se    second
 19 #define  rtl   rt<<1
 20 #define  rtr   rt<<1|1
 21 #define  bug                printf("******\n")
 22 #define  mem(a, b)          memset(a,b,sizeof(a))
 23 #define  name2str(x)        #x
 24 #define  fuck(x)            cout<<#x" = "<<x<<endl
 25 #define  sfi(a)             scanf("%d", &a)
 26 #define  sffi(a, b)         scanf("%d %d", &a, &b)
 27 #define  sfffi(a, b, c)     scanf("%d %d %d", &a, &b, &c)
 28 #define  sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
 29 #define  sfL(a)             scanf("%lld", &a)
 30 #define  sffL(a, b)         scanf("%lld %lld", &a, &b)
 31 #define  sfffL(a, b, c)     scanf("%lld %lld %lld", &a, &b, &c)
 32 #define  sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
 33 #define  sfs(a)             scanf("%s", a)
 34 #define  sffs(a, b)         scanf("%s %s", a, b)
 35 #define  sfffs(a, b, c)     scanf("%s %s %s", a, b, c)
 36 #define  sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
 37 #define  FIN                freopen("../in.txt","r",stdin)
 38 #define  gcd(a, b)          __gcd(a,b)
 39 #define  lowbit(x)          x&-x
 40 #define  IO                 iOS::sync_with_stdio(false)
 41 
 42 
 43 using namespace std;
 44 typedef long long LL;
 45 typedef unsigned long long ULL;
 46 const ULL seed = 13331;
 47 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
 48 const int maxn = 1e6 + 7;
 49 const int maxm = 8e6 + 10;
 50 const int INF = 0x3f3f3f3f;
 51 const int mod = 1e9 + 7;
 52 char s[maxn];
 53 LL sum[maxn];
 54 
 55 struct Palindrome_Automaton {
 56     int len[maxn], next[maxn][26], fail[maxn], cnt[maxn];
 57     int num [Max], S [Max], s, n, last;
58  
59      int newnode ( int l) {
 60          for ( int i = 0 ; i < 26 ; i ++) next [c] [i] = 0 ;
61          cnt [c] = num [c] = 0 , len [c] = l;
62          return c ++ ;
63      }
 64  
65      void init () {
 66          c = n last = 0 ;
67          newnode ( 0 );
68          newnode (-1);
 69         S[0] = -1;
 70         fail[0] = 1;
 71     }
 72 
 73     int get_fail(int x) {
 74         while (S[n - len[x] - 1] != S[n])x = fail[x];
 75         return x;
 76     }
 77 
 78     void add(int c, int pos) {
 79         c -= 'a';
 80         S[++n] = c;
 81         int cur = get_fail(last);
 82         if (!next[cur][c]) {
 83             int now = newnode(len[cur] + 2);
 84             fail[now] = next[get_fail(fail[cur])][c];
 85             next[cur][c] = now;
 86             num[now] = num[fail[now]] + 1;
 87         }
 88         last = next[cur][c];
 89         cnt[last]++;
 90     }
 91 
 92     void count()// number of occurrences of the same statistical nature palindromic sequence 
93      {
 94          for ( int I = SZ - . 1 ; I> = 0 ; Inc. (www.i-levelmedia.com)) CNT [Fail [I]] + = CNT [I];
 95          // reverse accumulation, will ensure that every point than the first count finished its parent node, a parent node can then applied to all descendants 
96      }
 97  } PAM;
 98  
99  int main () {
 100      //   the FIN; 
101      the while (~ SFS (S + . 1 )) {
 102          pam.init ();
 103          int n-= strlen (S + . 1 );
 104          for ( int I =1; i <= n; i++) {
105             pam.add(s[i], i);
106             sum[i] = sum[i - 1] + pam.num[pam.last];
107         }
108         pam.init();
109         LL ans = 0;
110         for (int i = n; i >= 1; --i) {
111             pam.add(s[i], i);
112             ans += pam.num[pam.last] * sum[i - 1];
113         }
114         printf("%lld\n", ans);
115     }
116     return 0;
117 }
View Code

 

Guess you like

Origin www.cnblogs.com/qldabiaoge/p/11403722.html