[传送门]

直接想最暴力的做法就是正解了。
每次询问都把两个串的回文树建出来,然后再两棵树上同时dfs,经过相同的节点答案就加一。遇到一个不存在的就退出。
再把询问记忆化一下就OK了。
复杂度是 $O(n \sqrt n)$
因为每次dfs的复杂度跟短的串的长度有关。
自己写给写T了。学习了下优秀写法。多开一个数组表示next是不是当前的next的。这样就不用每次都清数组了。

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std; const int N = 1e5 + ;
const int sz = ; struct PAM {
int s[N], fail[N], len[N];
pii ne[N][sz];
int n, tol, last, cnt;
void init() {
n = last = ;
tol = ;
s[] = len[] = -;
fail[] = ;
++cnt;
}
inline int getid(int x) {
while (s[n - len[x] - ] != s[n]) x = fail[x];
return x;
}
inline void extend(int c) {
s[++n] = c;
int cur = getid(last);
if (ne[cur][c].se != cnt) {
int k = getid(fail[cur]);
fail[++tol] = ne[k][c].se == cnt ? ne[k][c].fi : ;
ne[cur][c] = pii(tol, cnt); len[tol] = len[cur] + ;
}
last = ne[cur][c].fi;
}
} pam[]; vector<char> s[N];
map<pii, int> mp;
char str[N]; int ans; void dfs(int u, int v) {
for (int i = ; i < sz; i++) {
if (pam[].ne[u][i].se == pam[].cnt && pam[].ne[v][i].se == pam[].cnt) {
ans++;
dfs(pam[].ne[u][i].fi, pam[].ne[v][i].fi);
}
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%s", str);
int len = strlen(str);
for (int j = ; j < len; j++)
s[i].push_back(str[j]);
}
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
a ^= ans, b ^= ans;
if (s[a].size() > s[b].size()) swap(a, b);
if (mp.count(pii(a, b))) {
printf("%d\n", mp[pii(a, b)]);
continue;
}
pam[].init(), pam[].init();
int len = s[a].size();
for (int i = ; i < len; i++)
pam[].extend(s[a][i] - 'a');
len = s[b].size();
for (int i = ; i < len; i++)
pam[].extend(s[b][i] - 'a');
ans = ;
dfs(, ); dfs(, );
printf("%d\n", mp[pii(a, b)] = ans);
}
return ;
}

51nod1814 Clarke and string的更多相关文章

  1. hdu 5465 Clarke and puzzle 二维线段树

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  2. hdu 5626 Clarke and points 数学推理

    Clarke and points Problem Description   The Manhattan Distance between point A(XA,YA) and B(XB,YB) i ...

  3. hdu 5626 Clarke and points

    Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...

  4. hdu 5627 Clarke and MST(最大 生成树)

    Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...

  5. hdu 5564 Clarke and digits 矩阵快速幂优化数位dp

    Clarke and digits Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  7. BestCoder Round #56/hdu5463 Clarke and minecraft 水题

    Clarke and minecraft 问题描述 克拉克是一名人格分裂患者.某一天,克拉克分裂成了一个游戏玩家,玩起了minecraft.渐渐地,克拉克建起了一座城堡. 有一天,克拉克为了让更多的人 ...

  8. HDU 5565:Clarke and baton

    Clarke and baton  Accepts: 14  Submissions: 79  Time Limit: 12000/6000 MS (Java/Others)  Memory Limi ...

  9. HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和

    Clarke and digits  Accepts: 16  Submissions: 29  Time Limit: 5000/3000 MS (Java/Others)  Memory Limi ...

随机推荐

  1. Linux下常用目录有哪些?分别有什么作用?

    /boot:这个目录是用来存放与系统启动相关的文件 /root:root用户的家目录 /bin:存放大部分的二进制的可执行文件,也就是大部分的linux命令. /tmp:这个文件目录一般是公共的,也就 ...

  2. SQL Server DBCC命令大全

    原文出处:https://www.cnblogs.com/lyhabc/archive/2013/01/19/2867174.html DBCC DROPCLEANBUFFERS:从缓冲池中删除所有缓 ...

  3. 软件——解决Modelsim10.1d窗口不停弹出问题(一直弹窗)

    博主在编写Verilog HDL时需要用到Modelsim,于是博主便安装了Modelsim10.1d,然后兴高采烈打开准备跑仿真时,打开软件发现Modelsim10.1d的各种窗口在不停弹出,终止进 ...

  4. SQL系列(十四)—— 视图(view)

    说到视图view,大家应该都很熟悉.如几何学中用三视图来描述集合物体的外观构成,三视图中反应出物体的面貌.这里我们讨论数据库中视图的概念: 什么是视图 为什么会有会用视图 怎样使用视图 视图与表的异同 ...

  5. SpringBoot入门初体验

    概述 Java项目开发中繁多的配置,复杂的部署流程和第三方技术集成让码农在开发项目中效率低下,因此springBoot应运而生. 环境 IntelliJ IDEA 2018.3 jkd1.8 开始(傻 ...

  6. 上传文件时用form.submit提交的时候在低版本的IE中报拒绝访问的错误

    上传文件的时候,在IE7下总是传不了,但FireFox,IE11和Chrome下则可以上传.发现是form.submit();时出错了(“拒绝访问”). html代码为: <label oncl ...

  7. GIt 错误与常用命令

    命令和一些其他的属性等 *)在使用git commit -m “description" 这个描述会加在上次提交后所有add的文件后面,所以也可能产生不符合这个描述的文件后面也跟了这个描述, ...

  8. Linux下搭建keepalive+nginx

    一. 安装nginx(略) 二. 安装keepalive 下载http://www.keepalived.org/download.html 安装依赖包 yum install –y popt* gc ...

  9. Mycat分布式数据库架构解决方案--搭建MySQL读写分离环境--一主多从

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 本文主 ...

  10. Myeclipse6.5迁移到IDEA

    背景 myeclipse开发的javaweb项目用svn管理.现要转用idea开发.因为发现idea实在是太好用了.myeclipse6.5是个纯净版,用了两年,对于新手来说用myeclipse6.5 ...