UVALive 7041 The Problem to Slow Down You(回文树)
来源:互联网 发布:淘宝网店代销好吗 编辑:程序博客网 时间:2024/05/21 11:34
思路:一个回文树的应用...作为智障选手只会套版...
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define LL long longconst int MAXN = 400005 ;const int N = 26 ;LL ans = 0;struct Palindromic_Tree {int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点LL cnt[MAXN] ;int num[MAXN] ;int len[MAXN] ;//len[i]表示节点i表示的回文串的长度LL cnt1[MAXN];int S[MAXN] ;//存放添加的字符int last ;//指向上一个字符所在的节点,方便下一次addint n ;//字符数组指针int p ;//节点指针int newnode ( int l ) {//新建节点for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;cnt[p] = 0 ;cnt1[p] = 0;num[p] = 0 ;len[p] = l ;return p ++ ;}void init () {//初始化p = 0 ;newnode ( 0 ) ;newnode ( -1 ) ;last = 0 ;n = 0 ;S[0] = -1 ;//开头放一个字符集中没有的字符,减少特判fail[0] = 1 ;} void init1(){last = 0;S[0]=-1;fail[0]=1;n=0;}int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;return x ;}void add ( int c ) {c -= 'a' ;S[++ n] = c ;int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置if ( !(last = next[cur][c]) ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串int now = newnode ( len[cur] + 2 ) ;//新建节点fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转next[cur][c] = now ;//num[now] = num[fail[now]] + 1 ; last = now;}//last = next[cur][c] ;cnt[last] ++ ;} void add1 ( int c ) {c -= 'a' ;S[++ n] = c ;int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置if ( !(last = next[cur][c]) ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串int now = newnode ( len[cur] + 2 ) ;//新建节点fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转next[cur][c] = now ;//num[now] = num[fail[now]] + 1 ;last = now;}//last = next[cur][c] ;cnt1[last] ++ ;}void count () {for ( int i = p - 1 ; i >= 0 ; i-- ) cnt[fail[i]] += cnt[i];//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!}void count1 () {for ( int i = p - 1 ; i >= 0 ; -- i ) cnt1[fail[i]] += cnt1[i];//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!}void cal(){for(int i = 2;i<=p-1;i++)ans+=cnt[i]*cnt1[i];}}tree;char s1[200005];char s2[200005];int main(){ int T,cas=1;scanf("%d",&T);while(T--){ans = 0;tree.init();scanf("%s",s1);int len1 = strlen(s1); for(int i = 0;i<len1;i++)tree.add(s1[i]);tree.count();scanf("%s",s2);tree.init1(); int len2 = strlen(s2);for(int i = 0;i<len2;i++)tree.add1(s2[i]);tree.count1();tree.cal();printf("Case #%d: %lld\n",cas++,ans);}}
0 0
- UVAlive 7041 The Problem to Slow Down You(回文树)
- UVALive 7041 The Problem to Slow Down You(回文树)
- UVA 7041 The Problem to Slow Down You (回文树)
- Codeforce 2014ACM-ICPC, Asia Xian Regional Contest Problem G. The Problem to Slow Down You(回文树)
- Gym100548G-The Problem to Slow Down You
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(G - The Problem to Slow Down You-回文树)
- 2014 Asia Xian Regional Contest G The Problem to Slow Down You 回文串自动机
- 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You
- 【后缀树】acm 2014 西安赛区 Problem G. The Problem to Slow Down You
- 【codeforces】2014 Asia Xian Regional Contest G The Problem to Slow Down You 【Palindromic Tree】
- 2014 Asia Xian Regional Contest G The Problem to Slow Down You 【Palindromic Tree】
- [The Problem to Slow Down You] 后缀自动机 + 马拉车做法
- Codeforces Gym 100548G The Problem to Slow Down You (Palindromic Tree 或 Hash水过) 2014西安现场赛G题
- I read that if you specify too large a heap (using -Xmx) then it will slow down the application, is
- Don’t let jQuery’s $(document).ready() slow you down
- How to solve TFS slow response problem?
- Codeforces Gym 100548H The Problem to Make You Happy (图上博弈DP)
- UVALive 6342 The Mirror of Galadriel (回文串)
- Java同步
- BNUOJ 8108
- Spring TaskExecutor的用法
- 模仿JD导航界面
- php 如何获取文件的后缀名,比如图片扩展名jpg,gif等
- UVALive 7041 The Problem to Slow Down You(回文树)
- android 还可以输入多少个字符
- [MySQL]分组后查找每组的前N条记录语句
- 《CSS权威指南》学习记录——内边距、边框、外边距
- NSRange
- iOS开发 React-native开发环境配置
- 实习入职第十二天:MediaPlayer时间之间的转换函数
- STL系列之九 探索hash_set
- Java面向对象的三大特征