zoukankan      html  css  js  c++  java
  • I Love Palindrome String HDU

    题意:

    输出每个长度下的回文串(这些回文串的左半边也需要是回文串)

    题解:

    直接套上回文树,然后每找到一个回文串就将他hash。

    因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半边相同,

    自己画个图很容易发现的

    每次hash判断一下就好了

      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("******
    ")
     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 ULL p[maxn], HA[maxn];
     54 int ans[maxn];
     55 
     56 ULL get_HASH(int l, int r) {
     57     return HA[r] - HA[l - 1] * p[r - l + 1];
     58 }
     59 
     60 struct Palindrome_Automaton {
     61     int len[maxn], next[maxn][26], fail[maxn], cnt[maxn];
     62     int num[maxn], str[maxn], sz, n, last;
     63     int vis[maxn];
     64 
     65     int newnode(int l) {
     66         for (int i = 0; i < 26; ++i)next[sz][i] = 0;
     67         cnt[sz] = num[sz] = 0, len[sz] = l;
     68         return sz++;
     69     }
     70 
     71     void init() {
     72         sz = n = last = 0;
     73         newnode(0);
     74         newnode(-1);
     75         str[0] = -1;
     76         fail[0] = 1;
     77         mem(vis, 0);
     78     }
     79 
     80     int get_fail(int x) {
     81         while (str[n - len[x] - 1] != str[n])x = fail[x];
     82         return x;
     83     }
     84 
     85     void add(int c, int pos) {
     86         c -= 'a';
     87         str[++n] = c;
     88         int cur = get_fail(last);
     89         if (!next[cur][c]) {
     90             int now = newnode(len[cur] + 2);
     91             fail[now] = next[get_fail(fail[cur])][c];
     92             next[cur][c] = now;
     93             num[now] = num[fail[now]] + 1;
     94             int temp = (len[now] + 1) / 2;
     95             // fuck(n - len[now] + 1),fuck(n - len[now] + temp),fuck(n - temp),fuck(n);
     96             if (len[now] == 1 ||
     97                 get_HASH(n - len[now] + 1, n - len[now] + temp) == get_HASH(n - temp+1, n))
     98                 vis[now] = 1;
     99             else vis[now] = 0;
    100         }
    101         last = next[cur][c];
    102         cnt[last]++;
    103     }
    104 
    105     void count()//统计本质相同的回文串的出现次数
    106     {
    107         for (int i = sz - 1; i >= 0; --i) {
    108             cnt[fail[i]] += cnt[i];
    109             if (vis[i]) ans[len[i]] += cnt[i];
    110         }
    111         //逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
    112     }
    113 } pam;
    114 
    115 int main() {
    116 //    FIN;
    117     p[0] = 1;
    118     for (int i = 1; i < maxn; ++i) p[i] = p[i - 1] * seed;
    119     while (~sfs(s + 1)) {
    120         int n = strlen(s + 1);
    121         pam.init();
    122         for (int i = 1; i <= n; ++i) {
    123             HA[i] = HA[i - 1] * seed + s[i];
    124             pam.add(s[i], i);
    125             ans[i] = 0;
    126         }
    127         pam.count();
    128         for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], (i == n ? '
    ' : ' '));
    129     }
    130     return 0;
    131 }
    View Code
  • 相关阅读:
    【44.64%】【codeforces 743C】Vladik and fractions
    【47.40%】【codeforces 743B】Chloe and the sequence
    Java Web整合开发(18) -- Struts 2.x 标签
    os、sys及shutil
    Linux 下模拟Http 的get or post请求(curl和wget两种方法)
    密码正则
    主键唯一键重复插入解决方法
    php验证是否是中文
    nginx找不到php文件
    cannot get gid for group ‘nobody’
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/11403639.html
Copyright © 2011-2022 走看看