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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

UVAlive 7041 The Problem to Slow Down You(回文树)

發布時間:2024/4/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVAlive 7041 The Problem to Slow Down You(回文树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5053


先把第一個串插入回文樹中,然后把s數組清空插入第二個串,統計兩個cnt數組,答案是二者相乘的結果


#include <iostream> #include <stdlib.h> #include <algorithm> #include <stdio.h> #include <math.h> #include <string.h>using namespace std; typedef long long int LL; const int maxn=2e5+5; char str1[maxn]; char str2[maxn]; int n; LL ans; struct Tree {const static int maxn=4e5+5;int next[maxn][26];int fail[maxn];LL cnt[maxn];LL cnt2[maxn];int len[maxn];int s[maxn];int last,p,n;int new_node(int x){memset(next[p],0,sizeof(next[p]));cnt[p]=0;cnt2[p]=0;len[p]=x;return p++;}void init(){//memset(cnt,0,sizeof(cnt));//memset(cnt2,0,sizeof(cnt2));p=0;new_node(0);new_node(-1);last=0;n=0;s[0]=-1;fail[0]=1;}void init2(){last=0;s[0]=-1;fail[0]=1;n=0;}int get_fail(int x){while(s[n-len[x]-1]!=s[n])x=fail[x];return x;}void add(int x){x-='a';s[++n]=x;int cur=get_fail(last);if(!(last=next[cur][x])){int now=new_node(len[cur]+2);fail[now]=next[get_fail(fail[cur])][x];next[cur][x]=now;last=now;}cnt[last]++;}void add2(int x){x-='a';s[++n]=x;int cur=get_fail(last);if(!(last=next[cur][x])){int now=new_node(len[cur]+2);fail[now]=next[get_fail(fail[cur])][x];next[cur][x]=now;last=now;}cnt2[last]++;}void count(){for(int i=p-1;i>=0;i--)cnt[fail[i]]+=cnt[i];}void count2(){for(int i=p-1;i>=0;i--)cnt2[fail[i]]+=cnt2[i];}void fun(){for(int i=2;i<=p-1;i++){ans+=cnt[i]*cnt2[i];}}}tree; int main() {scanf("%d",&n);for(int j=1;j<=n;j++){scanf("%s%s",str1,str2);tree.init();int len=strlen(str1);int len1=strlen(str2);for(int i=0;i<len;i++){tree.add(str1[i]);}tree.count();tree.init2();ans=0;for(int i=0;i<len1;i++){tree.add2(str2[i]);}tree.count2();tree.fun();printf("Case #%d: %lld\n",j,ans);}return 0; }

轉載于:https://www.cnblogs.com/dacc123/p/8228629.html

總結

以上是生活随笔為你收集整理的UVAlive 7041 The Problem to Slow Down You(回文树)的全部內容,希望文章能夠幫你解決所遇到的問題。

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