SPOJ7258 SUBLEX - Lexicographical Substring Search
传送门[洛谷]
心态崩了我有妹子
靠 我写的记忆化搜索 莫名WA了 然后心态崩了
当我正要改成bfs排序的时候 我灵光一动 md我写的i=0;i<25;i++???
然后 改过来就A掉了T^T
大体做法就是 一个点出发的本质不同子串数量应该是就是所有添加字符的转移和其余选一个空串的转移
所以直接建出自动机然后 我的做法是直接记忆化搜索就可以省去建树/排序 因为所有子串必定由转移构成 所以可以直接记忆化
附代码。(我觉得这个做法巨强无比)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 20021225
#define ll long long
#define mxn 90010
using namespace std;
struct node{int ch[26],fa,len,f;}t[mxn*4];
int poi,cnt,lt,rt;char ch[mxn];
int id(char c){return c-'a';}
void insert(int c)
{
int p=lt,np=lt=++poi; t[np].len=t[p].len+1;
for(;p&&!t[p].ch[c];p=t[p].fa) t[p].ch[c]=np;
if(!p){t[np].fa=rt;return;}
int q=t[p].ch[c];
if(t[q].len==t[p].len+1){t[np].fa=q;return;}
int nq=++poi; t[nq].len=t[p].len+1;
memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
t[nq].fa=t[q].fa; t[q].fa=t[np].fa=nq;
for(;p&&t[p].ch[c]==q;p=t[p].fa) t[p].ch[c]=nq;
}
int query(int x)
{
if(~t[x].f) return t[x].f;
t[x].f=1;
for(int i=0;i<26;i++)
if(t[x].ch[i])
t[x].f+=query(t[x].ch[i]);
return t[x].f;
}
int n;
void getans(int pos,int k)
{
if(!k) return;
for(int i=0;i<26;i++)
if(t[pos].ch[i])
{
if(t[t[pos].ch[i]].f<k) k-=t[t[pos].ch[i]].f;
else{ch[++n]=i+'a';getans(t[pos].ch[i],k-1);break;}
}
}
int main()
{
scanf("%s",ch+1);n=strlen(ch+1);
rt=lt=++poi;for(int i=1;i<=n;i++) insert(id(ch[i]));
for(int i=1;i<=poi;i++) t[i].f=-1;query(rt);
//for(int i=1;i<=poi;i++) printf("%d\n",t[i].f);
int T;scanf("%d",&T);
while(T--)
{
for(int i=1;i<=n;i++) ch[i]=0;
int k;scanf("%d",&k);
n=0;getans(rt,k);
for(int i=1;i<=n;i++) printf("%c",ch[i]);
printf("\n");
}
return 0;
}
SPOJ7258 SUBLEX - Lexicographical Substring Search的更多相关文章
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- SPOJ SUBLEX Lexicographical Substring Search - 后缀数组
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...
- spoj SUBLEX (Lexicographical Substring Search) RE的欢迎来看看
SPOJ.com - Problem SUBLEX 这么裸的一个SAM,放在了死破OJ上面就是个坑. 注意用SAM做的时候输出要用一个数组存下来,然后再puts,不然一个一个字符输出会更慢. 还有一个 ...
- SPOJ:SUBLEX - Lexicographical Substring Search
题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...
- SP7258 SUBLEX - Lexicographical Substring Search
\(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求排名第k小的串 \(\color{#0066ff}{输入格式}\) 第一行给定主串(len<=90000) 第二行给定询 ...
- Spoj SUBLEX - Lexicographical Substring Search
Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
- SP7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
传送门 解题思路 首先建\(sam\),然后在拓扑序上\(dp\)一下,把每个点的路径数算出来,然后统计答案时就在自动机上\(dfs\)一下,仿照平衡树那样找第\(k\)小. 代码 #include& ...
随机推荐
- stack1顺序栈
顺序栈 #include<iostream> using namespace std; #define increasesize 10 template <class Object& ...
- JS实现多Div模块拖拽功能
空闲时间,同事让帮忙整个JS拖拽div模块功能.于是便在网上搜索,总结如下一个可实现多div模块拖拽的功能.一下是整体的HTML代码, 里边可以控制到 拖拽开始(onStart),拖拽时候(onMov ...
- http请求方法,get 对比 post
本文转自:http://www.w3school.com.cn/tags/html_ref_httpmethods.asp 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? ...
- leetcode 171. Excel表列序号(python)
给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
- redis 集群新增节点,slots槽分配,删除节点, [ERR] Calling MIGRATE ERR Syntax error, try CLIENT (LIST | KILL | GET...
redis reshard 重新分槽(slots) https://github.com/antirez/redis/issues/5029 redis 官方已确认该bug redis 集群重新(re ...
- vim-tabe多标签切换
vim-tabe多标签切换 本文转载自https://www.cnblogs.com/liqiu/archive/2013/03/26/2981949.html 1.新建标签页 使用:tabe命令和文 ...
- python网络编程中互斥锁与进程之间的通信
一.互斥锁 进程之间数据隔离,但是共享一套文件系统,因而可以通过文件来实现进程直接的通信,但问题是必须自己加锁处理. 注意:加锁的目的是为了保证多个进程修改同一块数据时,同一时间只能有一个修改,即串行 ...
- Spring Security 03
认证和鉴权 配置文件方式 <authentication-manager> <authentication-provider> <!-- 用户的权限控制 --> & ...
- Mybatis开发中前端控制器注解@Controller 引用的类错误
import org.springframework.web.portlet.ModelAndView; 错误 import org.springframework.web.servlet.Model ...
- python开发之路-day02
一.数据类型 1 什么是数据? name='sunkedong'#字符串类型 age=24 #整型 date=2017.9#浮点型 dic={'name':'sunkedong','age':16}# ...