HDU - 5394 Trie in Tina Town(回文自动机+字典树)
生活随笔
收集整理的這篇文章主要介紹了
HDU - 5394 Trie in Tina Town(回文自动机+字典树)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個字典樹,現在需要求出字典樹上所有的回文串做出的貢獻,為 出現次數*回文串長度,求出這個答案
題目鏈接:可以直接在字典樹上dfs然后維護貢獻,不過這就涉及到了回文自動機的刪除功能,其實實現起來很簡單,只需要將last變成數組記錄深度即可,每個節點的貢獻為其fail所指的結點的貢獻加上其本身的長度,因為是最長回文后綴,所以肯定包含了fail所指的結點的貢獻,再加上自己本身的長度就是每個節點的貢獻了
代碼:
#pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=2e6+100; //字符串長度最大值struct Edge {int to,next; }edge[N];int head[N],cnt;LL ANS;void addedge(int u,int v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++; }char s[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記錄了節點回文串的結束位置char s[N];int tot; // 節點個數int last[N]; // 上一個節點LL ans[N];int n;//當前字符串的長度 void init(){tot = n = 0;memset(fail, 0, sizeof fail);memset(cnt, 0, sizeof cnt);memset(sed, 0, sizeof sed);memset(len, 0, sizeof len);memset(nxt, 0, sizeof nxt);memset(ans,0,sizeof(ans));}void build(){len[0] = 0, len[1] = -1; // 0為偶數長度根, 1為奇數長度根tot = 1, last[0] = 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 deep){int c = ch - 'a';//全小寫要用a 全大寫要用A 不然會錯s[n=deep]=ch;int p = getfail(last[n-1], n);// 得到第i個字符可以加到哪個節點的兩端形成回文串if (!nxt[p][c]){tot++;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; // 新建節點ans[tot]=ans[fail[tot]]+len[tot];}last[n] = nxt[p][c]; // 當前節點成為上一個節點cnt[last[n]]++; //當前節點回文串++record[last[n]] = n;ANS+=ans[last[n]];}void get_cnt(){for (int i = tot; i > 0; i--)cnt[fail[i]] += cnt[i];//fail[i] 的節點 為 i 節點的后綴回文串, 所以個數相加} }tree;void dfs(int u,int deep) {for(int i=head[u];i!=-1;i=edge[i].next){int to=edge[i].to;tree.insert(s[to],deep);dfs(to,deep+1);} }void init() {tree.init();tree.build();memset(head,-1,sizeof(head));cnt=0;ANS=0; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){init();int n;scanf("%d",&n);for(int i=1;i<=n;i++){char ch[5];int fa;scanf("%s%d",ch,&fa);s[i]=ch[0];addedge(fa,i);}dfs(0,1);printf("%lld\n",ANS);}return 0; }?
總結
以上是生活随笔為你收集整理的HDU - 5394 Trie in Tina Town(回文自动机+字典树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU - 4300 Clairewd’
- 下一篇: 后缀自动机模板