http://blog.csdn.net/u013368721/article/details/42100363  回文树

建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就好了

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn=;
const int sign_size=;
struct Palind_Tree{
int next[maxn][sign_size];
int fail[maxn];
int cnt[maxn];
int num[maxn];
int len[maxn];
int S[maxn];
int last;
int n;
int p;
int newnode(int l)
{
for(int i=; i<sign_size; i++)next[p][i]=;
cnt[p]=;
num[p]=;
len[p]=l;
return p++;
}
void init()
{
p=;
newnode();
newnode(-);
last=;
n=;
S[n]=-;
fail[]=;
}
int get_fail(int x)
{
while(S[n-len[x]-]!=S[n])x=fail[x];
return x;
}
void add(int c)
{
c-='a';
S[++n]=c;
int cur=get_fail(last);
if(next[cur][c]==)
{
int now=newnode(len[cur]+);
fail[now]=next[get_fail( fail[cur] )][c];
next[cur][c]=now;
num[now]=num[fail[now]]+;
}
last=next[cur][c];
cnt[last]++;
}
void count()
{
for(int i=p-; i>=; --i)cnt[fail[i]]+=cnt[i];
}
}T1,T2;
char S[maxn],S2[maxn];
long long ans=;
void dfs(int u,int v)
{
for(int i=; i<sign_size; i++)
{
int x=T1.next[u][i],y=T2.next[v][i];
if(x&&y)
{
ans+=(long long)T1.cnt[x]*T2.cnt[y];
dfs(x,y);
}
}
}
int main()
{
int cas;
scanf("%d",&cas);
for(int cc=; cc<=cas; cc++)
{
scanf("%s%s",S,S2); T1.init();
T2.init();
int len1=strlen(S);
int len2=strlen(S2);
for(int i=; i<len1; i++)
T1.add(S[i]);
for(int i=; i<len2; i++)
T2.add(S2[i]);
T1.count();
T2.count();
ans=;
dfs(,);
dfs(,);
printf("Case #%d: %I64d\n",cc,ans);
}
return ;
}

2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You的更多相关文章

  1. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  2. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

  3. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  4. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  5. 2014-2015 ACM-ICPC, Asia Xian Regional Contest(部分题解)

    摘要 本文主要给出了2014-2015 ACM-ICPC, Asia Xian Regional Contest的部分题解,说明了每题的题意.解题思路和代码实现,意即熟悉区域赛比赛题型. Built ...

  6. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  7. 2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)

    2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) easy: ACE ...

  8. 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 ...

  9. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

随机推荐

  1. MySQL 批量写入数据报错:mysql_query:Lost connection to MySQL server during query

    场景: 批量往mysql replace写入数据时,报错. 解决方法: 1.增大mysql 数据库配置中 max_allowed_packet 的值 max_allowed_packet = 1G ( ...

  2. Gephi 网络图可视化工具

      官网:         https://gephi.org/   背景:         Gephi 是一款开源,免费, 跨平台的的graph 和 network 可视化工具,同时也提供了数据挖掘 ...

  3. SpringBoot系统列 3 - 多线程数据处理(ThreadPoolTaskExecutor、DruidDataSource)

    在上篇文章的基础上进行改造: package com.hello.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impo ...

  4. VS2013-2017 舒服的字体设置和背景色

    使用字体:Fixedsys Excelsior 3.01 1.如果没有安装字体的话,首先下载字体:http://www.fixedsysexcelsior.com/ 2.安装字体:控制面板 -> ...

  5. 如何查看SQL SERVER数据库当前连接数

    SELECT * FROM[Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID]FROM [Master].[dbo].[SYSDA ...

  6. 使用redis原生list结构作为消息队列取代celery框架。

    1.web后台对大批量的繁重的io任务需要解耦使用分布式异步技术,否则会使接口阻塞,并发延迟,一般就选celery好了.此篇的取代主要是针对取代celery的worker模式.没有涉及到周期和定时模式 ...

  7. miniprogrampatch 提供 watch 和 computed 特性

    推荐一个小程序补丁 github:miniprogrampatch,为你的 Page 和 Component 增加 watch 和 computed 特性. 安装 通过 npm 安装:npm inst ...

  8. Wireshark抓包分析TCP建立/释放链接的过程以及状态变迁分析

    Wireshark抓包分析TCP建立/释放链接的过程以及状态变迁分析 一.介绍计算机网络体系结构 1.计算机的网络体系结构 在抓包分析TCP建立链接之前首先了解下计算机的网络通信的模型,我相信学习过计 ...

  9. 萌新 学习 vuex

    vuex官网文档 https://vuex.vuejs.org/zh-cn/ 注: Mutation事件使用commit触发, Actions事件使用dispatch触发 安装 npm install ...

  10. Tesseract

    定义 Tesseract是一个将图像翻译成文字的OCR库(光学文字识别,Optical Character Recognition) 安装 sudo apt-get install tesseract ...