日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 932G Palindrome Partition(回文自动机+Palindrome Series优化dp)

發布時間:2024/4/11 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 932G Palindrome Partition(回文自动机+Palindrome Series优化dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為偶數的字符串,問將其分割成 k 個子串記為 a[ 1 ] , a[ 2 ] ... a[ k ] ,且滿足 a[ i ] == a[ k - i + 1 ] 的方案數是多少

題目分析:一個不太容易證明的轉換:

問題可以等價于,構造一個新的字符串 ss = s[ 1 ]s[ n ]s[ 2 ]s[ n - 1 ] ... s[ n/2 ]s[ n/2 + 1 ],使得分割的每一部分都是偶回文串的方案數

分割問題不難想到一個 n * n 的 dp,直接用回文自動機轉移即可,但很遺憾的是會超時

考慮優化,利用 Palindrome Series 可以將暴跳fail的時間復雜度優化成 logn,總時間復雜度優化成 O( nlogn )

代碼:
?

//#pragma GCC optimize(2) //#pragma GCC optimize("Ofast","inline","-ffast-math") //#pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;const int mod=1e9+7;char s[N],t[N];int n;LL f[N],g[N];struct Palindrome_tree {int nxt[N][26];int fail[N]; // 當前節點最長回文后綴的節點int len[N]; // 當前節點表示的回文串的長度int cnt[N]; // 當前節點回文串的個數, 在getcnt后可得到全部int sed[N]; // 以當前節點為后綴的回文串的個數(并不是表示第i結尾的回文串的種類數,如果要求每個點結尾的數的回文串個數,得用last)int record[N]; //record記錄了節點回文串的結束位置int diff[N],anc[N];char s[N];int tot; // 節點個數int last; // 上一個節點int n;//當前字符串的長度 void newnode(){tot++;memset(nxt[tot],0,sizeof(nxt[tot]));cnt[tot]=sed[tot]=len[tot]=fail[tot]=0;}void init(){n=0;tot=-1;newnode();newnode();len[0] = 0, len[1] = -1; // 0為偶數長度根, 1為奇數長度根tot = 1, last = 0;fail[0] = 1;}int getfail(int x, int n){while (s[n - len[x] - 1] != s[n]||n-len[x]-1<0) // 比較x節點回文串新建兩端是否相等//n-len[x]-1<0這個是我自己加的,多組的時候光第一個條件是不夠的,所以有錯請手動刪除x = fail[x]; // 若不同, 再比較x后綴回文串兩端return x;}void insert(char ch){int c = ch - 'a';//全小寫要用a 全大寫要用A 不然會錯s[++n]=ch;int p = getfail(last, n);// 得到第i個字符可以加到哪個節點的兩端形成回文串if (!nxt[p][c]){newnode();len[tot] = len[p] + 2; // 在p節點兩端添加兩個字符fail[tot] = nxt[getfail(fail[p], n)][c]; //tot點的后綴回文,可以由上一個節點的后綴回文嘗試得到sed[tot] = sed[fail[tot]] + 1; // 以當前節點為結尾的回文串個數nxt[p][c] = tot; // 新建節點diff[tot]=len[tot]-len[fail[tot]];anc[tot]=diff[tot]==diff[fail[tot]]?anc[fail[tot]]:fail[tot];}last = nxt[p][c]; // 當前節點成為上一個節點cnt[last]++; //當前節點回文串++record[last] = n;trans(n);}void trans(int i){for(int j=last;j>1;j=anc[j]){g[j]=f[i-len[anc[j]]-diff[j]];if(diff[j]==diff[fail[j]])g[j]=(g[j]+g[fail[j]])%mod; f[i]=(f[i]+(i%2==0)*g[j])%mod;}}void get_cnt(){for (int i = tot; i > 0; i--)cnt[fail[i]] += cnt[i];//fail[i] 的節點 為 i 節點的后綴回文串, 所以個數相加} }tree;int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);tree.init();scanf("%s",s+1);n=strlen(s+1);for(int i=1;i<=n/2;i++){t[2*i-1]=s[i];t[2*i]=s[n-i+1];}f[0]=1;for(int i=1;i<=n;i++)tree.insert(t[i]);printf("%lld\n",f[n]);return 0; }

?

總結

以上是生活随笔為你收集整理的CodeForces - 932G Palindrome Partition(回文自动机+Palindrome Series优化dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。