51nod1814 Clarke and string
[传送门]
直接想最暴力的做法就是正解了。
每次询问都把两个串的回文树建出来,然后再两棵树上同时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的更多相关文章
- hdu 5465 Clarke and puzzle 二维线段树
Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 5626 Clarke and points 数学推理
Clarke and points Problem Description The Manhattan Distance between point A(XA,YA) and B(XB,YB) i ...
- hdu 5626 Clarke and points
Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...
- hdu 5627 Clarke and MST(最大 生成树)
Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...
- hdu 5564 Clarke and digits 矩阵快速幂优化数位dp
Clarke and digits Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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思路差不 ...
- BestCoder Round #56/hdu5463 Clarke and minecraft 水题
Clarke and minecraft 问题描述 克拉克是一名人格分裂患者.某一天,克拉克分裂成了一个游戏玩家,玩起了minecraft.渐渐地,克拉克建起了一座城堡. 有一天,克拉克为了让更多的人 ...
- HDU 5565:Clarke and baton
Clarke and baton Accepts: 14 Submissions: 79 Time Limit: 12000/6000 MS (Java/Others) Memory Limi ...
- HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和
Clarke and digits Accepts: 16 Submissions: 29 Time Limit: 5000/3000 MS (Java/Others) Memory Limi ...
随机推荐
- [转帖]How long does it take to make a context switch?
How long does it take to make a context switch? FROM: http://blog.tsunanet.net/2010/11/how-long-do ...
- 模拟 + 打表 --- Emag eht htiw Em Pleh
Emag eht htiw Em Pleh Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2578 Accepted: ...
- Effective.Java第34-44条(枚举)
34. 使用枚举类型替代整型常量 常量的语义表达不清晰,只能靠前面的名称来区分.枚举具有可读性.更安全.更强大等优势.而且枚举类型对象之间的值比较可以使用==来比较值是否相等的,不是必须使用equa ...
- 删除排序链表中的重复元素II --链表
题目 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例1 输入: ->->->->->-> 输出: ->-> ...
- 你读过的最好的 C++ 开源代码是什么?
LevelDb LevelDb是谷歌两位大神级别的工程师发起的开源项目,简而言之,LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库.链接:google/level ...
- asp.net core mvc 集成miniprofiler
原文:asp.net core mvc 集成miniprofiler asp.net core mvc 集成miniprofiler 一.环境介绍 二.监控asp.net 页面 三.监控执行的sql语 ...
- 使用JavaConfig配置SpringMVC
目录结构 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...
- I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
遇到了这个问题,意思是你的 CPU 支持AVX AVX2 (可以加速CPU计算),但你安装的 TensorFlow 版本不支持 解决:1. 如果是初学者 或者 没有太大计算速度的需求,在开头加上这两行 ...
- elasticsearch 分词后聚合
es 对于text类型其实是分词存储的,但是有时候在聚合的时候,会发现这种情况下,会把字段分词后进行聚合.例如(1)A,B (2)B,C 然后聚合后B就是2个,A和C各一个. 这需要看业务需求了 ...
- mongodb 通过嵌入文档中的字段排序
mongodb中的全部数据: db.testInfo.find({}) .sort({_id:-1}) .limit(100) 查询结果: /* 1 createdAt:2019/10/11 下午5: ...