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 ...
随机推荐
- 桥接(Bridge)模式
桥接模式又称为柄体模式或接口模式.桥接模式的用意就是"将抽象化与实现化解耦,使得二者可以独立变化". 抽象化: 存在于多个实体中的共同的概念性联系,就是抽象化.作为一个过程,抽象化 ...
- 9. Scala隐式转换和隐式值
9.1 隐式转换 9.1.1 提出问题 先看一个案例演示,引出隐式转换的实际需要=>指定某些数据类型的相互转化 object boke_demo01 { def main(args: Array ...
- Gitbook常用命令
npm install gitbook-cli -g gitbook ls :列出本地安装版本: gitbook current :列出当前使用版本: gitbook ls-remote :列出远程可 ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- Mybatis+MySql 一个标签中执行多条sql语句
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/cxfly957/article/details/77896590 MySql默认是不支持这种骚操作的 ...
- Java数组转集合与集合转数组的坑
在Java中将数组转为集合,会用到Arrays.asList()的方法,然而,这个方法却与我们的预期期望存在一些出入,当用到asList方法将数组转化成List列表时,对得到的List列表进行add( ...
- C#完成 使用异步线程定时更新窗体标签内容,并对标签内容进行求和显示
这是我在面试过程中遇到的一个小测试,很可惜,当天未能圆满完成,虽然第二天经过实际测试已经OK 了,但学无止境,继续努力吧,特将此代码贴在这里,供以后学习使用: namespace mytest { p ...
- .Net Core2.2 在IIS发布
.Net Core应用发布到IIS主要是如下的三个步骤: (1)在Windows Server上安装 .Net Core Hosting Bundle (2)在IIS管理器中创建IIS站点 (3)部署 ...
- 使用基础知识完成java小作业?强化练习-1.输入数组计算最大值-2.输出数组反向打印-3.求数组平均值与总和-4.键盘输两int,并求总和-5.键盘输三个int,并求最值;
完成几个小代码练习?让自己更加强大?学习新知识回顾一下基础? 1.输入数组计算最大值 2.输出数组反向打印 3.求数组平均值与总和 4.键盘输两int,并求总和 5.键盘输三个int,并求最值 /* ...
- IT之快速提高效率的方法与思考
前言 文章也没什么很高深的问题,大概花个5分钟能看完.是一些大家都知道的道理,作为提醒与总结. 关于提高方面的内容,一般都有个人的方法,但大致都一致.可分为几个步骤. 框架.工具使用相关 使用框架.工 ...