zoukankan      html  css  js  c++  java
  • Palindrome Partition CodeForces

    题意:

    给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1]。问划分的方案数。 
    n<=1000000

    题解:

    反正我是不会做   (我是转载的yyb博客,巨佬写的超级超级详细
    基本就是照着laofulaofu的打了一遍(laofu太强啦)

    这题分成了两个步骤
    如果直接分kk段我们是没法直接判断的
    假设两段si,ski+1
    因为si=ski+1=x1x2.....xj  
    假设si的开始位置为p
    假设原串S的长度为n
    si=S[p]S[p+1]....S[p+j1]
    ski+1=S[njp+1]S[njp+2]...
    对应相等的关系是
    S[p]=S[npj+1]
    如果p=1考虑一下特殊情况
    那么就是S[1]=S[nj]
    那么,如果我们有一个串S
    S=S[1]S[n]S[2]S[n2].....
    那么,对应到上面的相等关系里面,
    就是S[1..j]是一个回文串
    其他的每一组对应关系也是如此
    所以题目转换成了:
    告诉你了S回答把S分为若干个长度为偶数的回文串的方案数
    (如果没有搞清楚可以手玩一下)

    这个就是一个裸的dp
    f[i]f[i]表示S[1..i]S′[1..i]划分为若干长度为偶数的回文串的方案数
    得到转移方程:

    f[i]=jf[j1]


    其中,S[j,i]S′[j,i]是回文串
    那么,每一个jj对应的位置相当于是S[1..i]S′[1..i]的回文后缀
    构建出回文树之后沿着lastfail就可以得到所有的j的位置

    这样子的复杂度是O(回文串数量)的,显然会超时,考虑如何优化。 
    有一个小结论就是一个回文串S的一个后缀T是回文串当且仅当T是S的border。 
    有一个性质就是一个串的所有border可以划分成O(log)段等差数列。 

    维护pre[p]表示节点p下一个等差数列的末项。 d[p]表示公差

      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 
     53 struct Palindrome_Automaton {
     54     int len[maxn], next[maxn][26], fail[maxn], cnt[maxn];
     55     int num[maxn], S[maxn], sz, n, last, d[maxn], pre[maxn];
     56 
     57     int newnode(int l) {
     58         for (int i = 0; i < 26; ++i)next[sz][i] = 0;
     59         cnt[sz] = num[sz] = 0, len[sz] = l;
     60         return sz++;
     61     }
     62 
     63     void init() {
     64         sz = n = last = 0;
     65         newnode(0);
     66         newnode(-1);
     67         S[0] = -1;
     68         fail[0] = 1;
     69     }
     70 
     71     int get_fail(int x) {
     72         while (S[n - len[x] - 1] != S[n])x = fail[x];
     73         return x;
     74     }
     75 
     76     void add(int c, int pos) {
     77         c -= 'a';
     78         S[++n] = c;
     79         int cur = get_fail(last);
     80         if (!next[cur][c]) {
     81             int now = newnode(len[cur] + 2);
     82             fail[now] = next[get_fail(fail[cur])][c];
     83             next[cur][c] = now;
     84             num[now] = num[fail[now]] + 1;
     85             d[now] = len[now] - len[fail[now]];
     86             pre[now] = (d[now] == d[fail[now]] ? pre[fail[now]] : fail[now]);
     87         }
     88         last = next[cur][c];
     89         cnt[last]++;
     90     }
     91 
     92 } pam;
     93 
     94 char str[maxn], s[maxn];
     95 LL dp[maxn], f[maxn];
     96 
     97 int main() {
     98 //    FIN;
     99     sfs(str + 1);
    100     int len = strlen(str + 1), n = 0;
    101     for (int i = 1; i <= len; i += 2) s[i] = str[++n];
    102     n = len;
    103     for (int i = 2; i <= len; i += 2) s[i] = str[n--];
    104     dp[0] = 1;
    105     pam.init();
    106     for (int i = 1; i <= len; ++i) {
    107         pam.add(s[i], i);
    108         for (int p = pam.last; p; p = pam.pre[p]) {
    109             f[p] = dp[i - pam.len[pam.pre[p]] - pam.d[p]];
    110             if (pam.d[p] == pam.d[pam.fail[p]]) f[p] = (f[p] + f[pam.fail[p]]) % mod;
    111             if (i % 2 == 0) dp[i] = (dp[i] + f[p]) % mod;
    112         }
    113     }
    114     printf("%lld
    ", dp[len]);
    115     return 0;
    116 }
    View Code
  • 相关阅读:
    C#中二进制,八进制,十六进制到十进制的相互转换
    Mac装Win10后没有无线网络的处理
    U盘容纳不了大于4G的文件比如ISO文件咋办?
    经典游戏“大富翁4”存档文件修改器Rich4Editor下载
    向C#的选项卡中添加自定义窗体
    C#对二进制文件的特定位置进行读写小结
    抗战剧中最耐看的《我的团长我的团》,最后结尾依然有神剧的影子
    绝大多数人努力程度之低,根本轮不上拼天赋
    ZT:与其怨天尤人,不如全力以赴;若想改变世界,你必须先从改变自己开始!
    java基础学习_多线程02_多线程、设计模式_day24总结
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/11403862.html
Copyright © 2011-2022 走看看