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. MyBatis ResultMap Assocation 返回属性为null的问题

    Model: public class Employee { private Integer id; private String lastName; private String email; pr ...

  2. ABAP 文件选择框

    GUI_FILE_SAVE_DIALOG    CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE         EXPORTING ...

  3. HAWQ配置之客户端访问

    一.配置简单的口令访问 1,建立数据库和用户 postgres=# create database epbd; CREATE DATABASE postgres=# create role user1 ...

  4. 蜕变成蝶~Linux设备驱动之watchdog设备驱动

    看门狗(watchdog )分硬件看门狗和软件看门狗.硬件看门狗是利用一个定时器 电路,其定时输出连接到电路的复位端,程序在一定时间范围内对定时器清零 (俗称 “喂狗”),如果程序出现故障,不在定时周 ...

  5. 一个简易的netty udp服务端

    netty号称java高性能网络库,为人帮忙中,研究了下,写了一个demo.反复调试,更改,局域网两个客户端同时for循环发10000个20字节的数据包,入库mysql,居然没丢. 思路,netty的 ...

  6. VS2017 配置QT5

    QT安装 1. QT下载 2. 安装过程中,组件的选择(图自https://blog.csdn.net/gaojixu/article/details/82185694) 3. 安装完成 VS2017 ...

  7. php四个常用类封装

    这4个类分别是Mysql类. 分页类.缩略图类.上传类. Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = nul ...

  8. hbase region still in transition

    1,删除hbase中的 hbase:meta表中相应的region的row 如; get 'hbase:meta','kylin_metadata,,1481101316881.f3b4c7c1148 ...

  9. Solve Error: nafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmt.lib(newaop.obj)

    Error: nafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)&quo ...

  10. plsql中文乱码

    一.关于PLSQL无法正确显示中文 刚才下载安装了PLSQL Developer 9.0.0.1601 汉化绿色版,执行SQL查询语句,发现显示的数据中只要有中文都会以?表示. 原因:客户端跟服务器的 ...