题目链接:

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(回文树)相关推荐

  1. 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树

    The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...

  2. 2018 ICPC 南京 M. Mediocre String Problem(ExKMP + Manacher / ExKMP+回文树)

    2018 ICPC 南京 全文见:https://blog.csdn.net/qq_43461168/article/details/112796538 M. Mediocre String Prob ...

  3. Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢Grunt大佬的细心讲解)...

    layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...

  4. Problem K. Road Network - ACPC2015(求树的直径 dp)

    Problem K. Road Network 题目链接 Arab Collegiate Programming Contest 2015 题意: 树形的公路,如果阻塞道路可以断开两个区域彼此的连接, ...

  5. codeforce 570 problem E 51Nod-1503-猪和回文

    1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...

  6. BZOJ.3218.a + b Problem(最小割ISAP 可持久化线段树优化建图)

    BZOJ UOJ 首先不考虑奇怪方格的限制,就是类似最大权闭合子图一样建图. 对于奇怪方格的影响,显然可以建一条边\((i\to x,p_i)\),然后由\(x\)向\(1\sim i-1\)中权值在 ...

  7. A Simple Problem with Integers POJ - 3468(线段树+区间查询+区间修改+建树+懒惰标记模板)+(树状数组)

    题意: 有一个数组,有两种操作.1: Q a b 求[a,b]的和 2:C a b c 给[a,b] 的所有元素都加上c. 题目: You have N integers, A1, A2, ... , ...

  8. Problem C: 判断字符串是否为回文

    #include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); ...

  9. 2018ICPC南京 Problem M. Mediocre String Problem(回文串,马拉车,扩展KMP)

    题意: 给你两个字符串 s , t s,t s,t,要求从 s s s中找到一个子串和 t t t的一个前缀拼起来,结果要是回文串.求多少种拼法. 思路: 借此题复习了一下字符串算法. 首先 s s ...

最新文章

  1. RunLoop 详解
  2. eeglab中文教程系列(18)-自己创建电极位置以及电极位置文件
  3. Hibernate 集合映射
  4. C++ 异常 与 ”为什么析构函数不能抛出异常“ 问题
  5. 【单片机实验】矩阵键盘
  6. Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案
  7. 安卓学习笔记28:文件流操作
  8. 主角把异能开发计算机,高等数学上下
  9. 服务器系统安装蓝牙驱动,win10蓝牙驱动怎么安装?-win10蓝牙驱动的安装教程 - 河东软件园...
  10. 数学建模题目和模型汇总(2000-2019 国赛本科组)
  11. 浪曦struts2学习笔记1
  12. 45+最佳免费WordPress主题(2021年)
  13. mysql otl变量绑定_otl_stream流相关绑定变量
  14. 华为云centos8 配置国内yum源及遇到问题
  15. python手游自动化测试流程_基于Python+appium的ios自动化测试demo(更新中)
  16. 【java】生成一个均匀分布的参考平面
  17. 怎么安装vs2013配套的VASSISTX番茄助手
  18. 致远项目管理SPM系统资源管理之资源台班工时管理
  19. rust高墙大门怎么造_rust自己建的高墙怎么拆 | 手游网游页游攻略大全
  20. 京东C2M的“马桶故事”

热门文章

  1. 【MFC】对话框中创建工具栏
  2. 【工业控制】学习喷墨打印技术 怎么能不知道波形
  3. 【Tiny4412】烧写Qt文件系统到EMMC
  4. 【Linux网络编程】无连接和面向连接协议的区别
  5. python中用来回溯异常的模块_python学习笔记(异常)
  6. mybatis源码刨析总结
  7. vscode could not establish connection to linux The VS Code Server failed to start
  8. 二叉树的直径—leetcode543
  9. framework dyld: Symbol not found: _OBJC_CLASS_xxx
  10. MFC使用CCommandLineInfo处理命令行