[传送门]

直接想最暴力的做法就是正解了。
每次询问都把两个串的回文树建出来,然后再两棵树上同时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. python 计算列表内容出现次数

    """python 计算列表内容出现次数""" #方法一: l = ['a','a','b','c','d','b','b','b'] te ...

  2. 迭代子(Iterator)模式

    迭代子模式又叫做游标模式.迭代子模式可以顺序地访问一个聚集中的元素而必暴露聚集的内部表象. 1.  聚集和Java聚集 多个对象在一起形成的总体形成聚集(Aggregate),聚集对象是能够包容一组对 ...

  3. Sitecore 8.2 防火墙规则的权威指南

    如今,使用多层安全保护您的数据不再是奢侈品; 这是不容谈判的.此外,您需要确保Sitecore解决方案保持运行并与集成服务(例如SQL,Mongo,Solr)通信,同时保持相同的安全级别. 让我们假设 ...

  4. 关于Java单例模式中双重校验锁的实现目的及原理

    开始复习设计模式,一开始理解单例模式中的双重校验锁卡住了,想通了后就自己做了段思维导图来帮助自己理解. 其实理解下来并不难,但还是记录下来帮助自己回忆和借机试试养成写博客的习惯~ public cla ...

  5. [转] service worker初探:超级拦截器与预缓存

    在2014年,W3C公布了service worker的草案,service worker提供了很多新的能力,使得web app拥有与native app相同的离线体验.消息推送体验. service ...

  6. KSQL: Streaming SQL for Apache Kafka

    Few weeks back, while I was enjoying my holidays in the south of Italy, I started receiving notifica ...

  7. 动软软件 生成 实体类模板(EnterpriseFrameWork框架)

    1.废话不多说,直接上效果图 . 2 .动软模板代码 <#@ template language="c#" HostSpecific="True" #&g ...

  8. Docker安装Consul集群

    Docker 安装Consul集群 使用windows 环境,Docker desktop community 构建consul集群. 1.docker 容器网络 docker安装后,默认会创建三种网 ...

  9. vue条形码生成插件vue-barcode

    更详细的请查阅官方文档 https://github.com/lindell/vue-barcode vue-barcode是JsBarcode的一个简单包装.所以在使用时的配置属性需要在JsBarc ...

  10. vue路由切换时内容组件的滚动条回到顶部

    在使用vue的时候会出现切换路由的时候滚动条保持在原来的位置,要切换路由的时候滚动条回到顶部才有更好的用户体验 1.当页面整体都要滚动到顶部的情况 router.afterEach(() => ...