[传送门]

直接想最暴力的做法就是正解了。
每次询问都把两个串的回文树建出来,然后再两棵树上同时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. react项目中怎么使用http-proxy-middleware反向代理跨域

    第一步 安装 http-proxy-middleware npm install http-proxy-middleware 我们这里面请求用的axios,在将axios安装一下 npm instal ...

  2. 【转帖】nmap命令总结

    nmap命令总结 https://www.cnblogs.com/chenqionghe/p/10657722.html 一.nmap是什么 nmap是一款网络扫描和主机检测的非常有用的工具,不局限于 ...

  3. Python之路【第二十五篇】:数据库之pymysql模块

    数据库进阶 一.pymysql模块 pymysql是Python中操作Mysql的模块,其使用的方法和py2的MySQLdb几乎相同. 二.pymysql模块安装 pip install pymysq ...

  4. SQL Server外键关系是强制约束,外键值也可以是空(NULL)

    在SQL Server中,实际上外键值可不可以为空(NULL),和外键关系是不是强制约束无关. 我们先在SQL Server数据库中建立两张表People和Car,一个People可以有多个Car,所 ...

  5. axios捕获401 赋值token

    //捕获401 // http request 拦截器 axios.interceptors.request.use( config => { const token = localStorag ...

  6. jsGrid使用入门

    jsGrid使用入门 原创蓝天上的一朵云 本文链接:https://blog.csdn.net/u012846041/article/details/82735811 jsGrid资源地址: http ...

  7. Spring MVC Web.xml配置

    Web.xml spring&spring mvc 在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个 ...

  8. 把EXECL表格导入到WORD中

    一般我们在编写开发文档时需要进行表格导入导出,这里提供几种方法供参考. 法一: 打开EXECL,WORD软件,在需要导入表格的地方选择“插入” ,找到“对象选项: ”在对象对话框中点击“由文件创建”, ...

  9. 编写可维护的JavaScript-随笔(一)

    一.基本的格式化 1. 缩进层级 a)      制表符缩进 i.          好处:制表符和缩进层级是一对一的关系是符合逻辑的,文本编辑器可以配置制表符的展示长度,可以随意调节 ii.     ...

  10. Vue学习之监听methods、watch及computed比较小结(十一)

    一.三者之间的对比: 1.methods方法表示一个具体的操作,主要书写业务逻辑: 2.watch:一个对象,键是需要观察的表达式,值是对应回调函数.主要用来监听某些特定数据的变化,从而进行某些具体业 ...