2016集训测试赛(十九)Problem C: 无聊的字符串

Solution
傻X题
我的方法是建立后缀后缀树, 然后在DFS序列上直接二分即可.
关键在于如何得到后缀树上每个字符对应的字节点: 我们要在后缀自动机上记录每个点在后缀树上对应的字母. 考虑如何实现, 我们在后缀自动机上的每个状态上, 记录其所对应的在字符串中的位置, 减去其父亲节点的长度即可得到每个节点对应的后缀树上的字符.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int LEN = (int)2e5;
struct suffixAutomaton
{
int tp, rt, L, R;
struct node
{
int suc[26], pre;
int endPosition, len, c;
int vst;
int successorOnSuffixTree[26];
inline node() {memset(suc, -1, sizeof(suc)); pre = -1; vst = 0; memset(successorOnSuffixTree, -1, sizeof(successorOnSuffixTree));}
}nd[LEN << 1];
void DFS(int u)
{
nd[u].vst = 1; if(u != rt) nd[nd[u].pre].successorOnSuffixTree[nd[u].c] = u;
for(int i = 0; i < 26; ++ i) if(~ nd[u].suc[i] && ! nd[nd[u].suc[i]].vst) DFS(nd[u].suc[i]);
}
int clk;
int idx[LEN << 1], sz[LEN << 1];
long long sum[LEN << 1];
inline void getId(int u)
{
int cur = clk ++;
idx[cur] = u; sz[cur] = u == rt ? 0 : nd[u].len - nd[nd[u].pre].len;
for(int i = 0; i < 26; ++ i) if(~ nd[u].successorOnSuffixTree[i])
{
int v = nd[u].successorOnSuffixTree[i];
getId(v);
if(nd[u].endPosition == -1 || nd[u].endPosition > nd[v].endPosition) nd[u].endPosition = nd[v].endPosition;
}
}
inline void build(char *str, int len)
{
L = R = 0;
rt = 0; nd[rt].len = 0; tp = 1;
int lst = rt;
for(int i = len - 1; ~ i; -- i)
{
char c = str[i] - 'a';
int u = tp ++; nd[u].len = nd[lst].len + 1; nd[u].endPosition = i;
for(; ~ lst && nd[lst].suc[c] == -1; lst = nd[lst].pre) nd[lst].suc[c] = u;
if(lst == -1) nd[u].pre = rt, nd[u].c = str[i] - 'a';
else
{
int p = nd[lst].suc[c];
if(nd[p].len == nd[lst].len + 1) nd[u].pre = p, nd[u].c = str[i + nd[p].len] - 'a';
else
{
int q = tp ++;
memcpy(nd[q].suc, nd[p].suc, sizeof(nd[p].suc)); nd[q].pre = nd[p].pre; nd[q].len = nd[lst].len + 1; nd[q].endPosition = i;
// we must set nd[q].endPosition's value right now, 'cause it may be used later
nd[q].c = str[i + nd[nd[q].pre].len] - 'a';
nd[p].pre = nd[u].pre = q;
nd[p].c = str[nd[p].endPosition + nd[q].len] - 'a'; nd[u].c = str[i + nd[q].len] - 'a';
for(; ~ lst && nd[lst].suc[c] == p; lst = nd[lst].pre) nd[lst].suc[c] = q;
}
}
lst = u;
}
DFS(rt);
clk = 0; getId(rt);
sum[0] = sz[0]; for(int i = 1; i < tp; ++ i) sum[i] = sum[i - 1] + sz[i];
}
inline void query(long long k)
{
k = (long long)(L ^ R ^ k) + 1;
// int k = v;
int curL = 0, curR = tp - 1, pos = -1;
while(curL <= curR)
{
int mid = curL + curR >> 1;
if(sum[mid] >= k) {pos = mid; curR = mid - 1;} else curL = mid + 1;
}
if(pos == -1) {L = 0; R = 0; printf("%d %d\n", L, R); return;}
L = nd[idx[pos]].endPosition + 1; R = L + (k - sum[pos - 1] + nd[nd[idx[pos]].pre].len) - 1;
printf("%d %d\n", L, R);
}
}SAM;
int main()
{
#ifndef ONLINE_JUDGE
freopen("string.in", "r", stdin);
freopen("string.out", "w", stdout);
#endif
static char str[LEN]; scanf("%s", str);
SAM.build(str, strlen(str));
int m; scanf("%d", &m);
long long v;
for(int i = 0; i < m; ++ i) scanf("%lld", &v), SAM.query(v);
}
2016集训测试赛(十九)Problem C: 无聊的字符串的更多相关文章
- 2016集训测试赛(十九)Problem A: 24点大师
Solution 这到题目有意思. 首先题目描述给我们提供了一种非常管用的模型. 按照题目的方法, 我们可以轻松用暴力解决20+的问题; 关键在于如何构造更大的情况: 我们发现 \[ [(n + n) ...
- 2016集训测试赛(二十六)Problem A: bar
Solution 首先审清题意, 这里要求的是子串而不是子序列... 我们考虑用1表示p, -1表示j. 用sum[i]表示字符串前\(i\)的前缀和. 则我们考虑一个字符串\([L, R]\)有什么 ...
- 2016集训测试赛(二十四)Problem B: Prz
Solution 这道题有两个关键点: 如何找到以原串某一个位置为结尾的某个子序列的最晚出现位置 如何找到原串中某个位置之前的所有数字的最晚出现位置中的最大值 第一个关键点: 我们注意到每个数字在\( ...
- 2016集训测试赛(二十四)Problem C: 棋盘控制
Solution 场上的想法(显然是错的)是这样的: 我们假设棋子是一个一个地放置的, 考虑在放置棋子的过程中可能出现哪些状态. 我们令有序整数对\((i, j)\)表示总共控制了\(i\)行\(j\ ...
- 2016集训测试赛(二十)Problem B: 字典树
题目大意 你们自己感受一下原题的画风... 我怀疑出题人当年就是语文爆零的 下面复述一下出题人的意思: 操作1: 给你一个点集, 要你在trie上找到所有这样的点, 满足点集中存在某个点所表示的字符串 ...
- 2016集训测试赛(二十)Problem A: Y队列
Solution 考虑给定一个\(n\), 如何求\(1\)到\(n\)的正整数中有多少在队列中. 不难注意到我们只需要处理质数次方的情况即可, 因为合数次方会被其因数处理到. 同时我们考虑到可能存在 ...
- 2016集训测试赛(十八)Problem C: 集串雷 既分数规划学习笔记
Solution 分数规划经典题. 话说我怎么老是忘记分数规划怎么做呀... 所以这里就大概写一下分数规划咯: 分数规划解决的是这样一类问题: 有\(a_1, a_2 ... a_n\)和\(b_1, ...
- 2016北京集训测试赛(九)Problem C: 狂飙突进的幻想乡
Solution 我们发现, 对于一条路径来说, 花费总时间为\(ap + q\), 其中\(p\)和\(q\)为定值. 对于每个点, 我们有多条路径可以到达, 因此对于每个区间中的\(a\)我们可以 ...
- 2016集训测试赛(二十一)Problem C: 虫子
题目大意 给你一棵树, 每个点有一个点权. 有两种操作: link / cut 修改某个点的点权 每次操作后, 你要输出以下答案: 在整棵树中任意选两个点, 这两个点的LCA的期望权值. Soluti ...
随机推荐
- Codeforces 653G Move by Prime 组合数学
题意: 有一个长度为\(n\)的正整数序列\(a\),有这样一种操作: 每次可以选序列中的某一个数乘上或除以某一个素数. 求对于每一个子序列使其所有元素相等的最少操作次数之和. 分析: 因为两个素数之 ...
- 03015_JSTL技术
1.JSTL概述 (1)JSP(JSP Standard Tap Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要替代js ...
- laravel5.2总结--集合
类(Laravel集合基类) Illuminate\Support\Collection 类提供一个流畅.便利的封装来操控数组数据,官方提供了很多辅助函数,方便对数据进行各种处理,Coll ...
- 数据库路由中间件MyCat - 使用篇(1)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 基本概念 直接介绍概念太枯燥了,还是拿个和背景篇相似的例子介绍 业务场景:客户完成下单,快递员接受并更新运单 ...
- Asp.net Mvc 页面静态化
http://www.cnblogs.com/gowhy/archive/2013/01/01/2841472.html
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- copy & deepcopy
1 import copy 2 3 字典参照列表结论,看是否有深层嵌套. 4 a = {'name':1,'age':2} 5 b = a 6 a['name'] = 'ff' 7 print(a) ...
- aspx页面直接访问后台方法
在方法上面机上[WebMethod]就可以直接请求该方法了.
- vue中echarts 在element-ui的tab 切换时 width 为100px 时的解决方式
最近在项目中遇到了这种情况,需要在tab控件上渲染多个echart图标,然后切换查看时,发现图表的宽度不正确 原因:在页面进行加载时,隐藏的图表找不到对应的div大小,所以默认给了一个大小.所以要做的 ...
- RazorExtensions Templated Razor Delegates
原文发布时间为:2011-04-27 -- 来源于本人的百度文章 [由搬家工具导入] Templated Razor Delegates David Fowler turned me on to a ...