[SPOJ-BEADS]Glass Beads
来源:
CE1998
题目大意:
求字符串最小表示。
思路:
字符串复制一遍接在后面,构建SAM,然后每次跑小的转移。
跑n次以后就跑到了最小表示的末尾,用该状态的len值减去n就是最小表示的起始位置。
#include<string>
#include<iostream>
int n;
std::string s;
class SuffixAutomaton {
private:
static const int SIGMA_SIZE=;
struct State {
State *link,*trans[SIGMA_SIZE];
unsigned len,min_trans;
State(const int l) {
link=nullptr;
std::fill(&trans[],&trans[SIGMA_SIZE],nullptr);
min_trans=SIGMA_SIZE;
len=l;
}
};
State *root,*last;
unsigned idx(const int ch) {
return ch-'a';
}
void extend(const char ch) {
const unsigned w=idx(ch);
State *p=last,*new_p=new State(last->len+);
while(p!=nullptr&&p->trans[w]==nullptr) {
p->trans[w]=new_p;
p->min_trans=std::min(p->min_trans,w);
p=p->link;
}
if(p==nullptr) {
new_p->link=root;
} else {
State *q=p->trans[w];
if(q->len==p->len+) {
new_p->link=q;
} else {
State *new_q=new State(p->len+);
std::copy(&q->trans[],&q->trans[SIGMA_SIZE],new_q->trans);
new_q->min_trans=q->min_trans;
new_q->link=q->link;
q->link=new_p->link=new_q;
while(p!=nullptr&&p->trans[w]==q) {
p->trans[w]=new_q;
p=p->link;
}
}
}
last=new_p;
}
public:
void build() {
root=last=new State();
for(std::string::iterator i=s.begin();i<s.end();i++) {
extend(*i);
}
for(std::string::iterator i=s.begin();i<s.end();i++) {
extend(*i);
}
}
unsigned query() {
State *p=root;
for(unsigned i=;i<s.length();i++) {
p=p->trans[p->min_trans];
}
return p->len-s.length()+;
}
};
SuffixAutomaton sam;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cin>>n;
for(int i=;i<n;i++) {
std::cin>>s;
sam.build();
std::cout<<sam.query()<<std::endl;
}
return ;
}
[SPOJ-BEADS]Glass Beads的更多相关文章
- POJ1509 Glass Beads
Glass Beads Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4314 Accepted: 2448 Descr ...
- POJ1509 Glass Beads(最小表示法 后缀自动机)
Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4901 Accepted: 2765 Description Once ...
- 【POJ1509】Glass Beads
[POJ1509]Glass Beads [题目描述]给定字符串S,并规定首尾相连成环,求出最小字典序. [输入]输入有多个数据,第一行只包括正整数N,表示有N组数据.每个数据包括一行,输入该字符串. ...
- zoj 2006 Glass Beads
Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006 Time Limit: 2 Seconds ...
- cogs 2123. [HZOI 2015] Glass Beads
2123. [HZOI 2015] Glass Beads ★★★ 输入文件:MinRepresentations.in 输出文件:MinRepresentations.out 简单对比时 ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...
- POJ 1509 Glass Beads
Description 求字符串的最小循环表示. Sol SAM. 把原串复制一遍,建出SAM,然后每次选最小的一个跑 \(len\) 次,这就是最小循环表示的最后一个节点,然后 \(x-len+1\ ...
- 1509 -- Glass Beads POJ
题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...
- 杂项(最小表示法):HZOI 2015 Glass Beads
[题目描述] 给定长度为n(n<=300000)的循环同构的字符串,定义最小表示为该字符串的字典序最小的同构表示,请输出这个表示. [输入格式] 第一行是串的长度,第二行是字符串. [输出格式] ...
- UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)
题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...
随机推荐
- Win7下VS2010不能链接问题
装了2012准备学VC++窗体开发,然后发现手边只有VS2010的教程,于是卸掉VS2012改装VS2010,结果发现不管写啥,链接时都报错“error Link1123 转到coff期间失败”. 于 ...
- python端口扫描
简易版: #author:Blood_Zero #coding:utf-8 import socket import sys PortList=[21,22,23,25,80,135] # host= ...
- 基于Netty4.1.29.Final的helloworld实现.使用idea
服务端: //服务端 public class Server { public static void main(String[] args) { //创建两个线程组 EventLoopGroup c ...
- 邮件伪造测试-Swaks
1. 前言 在kali中自带一个邮件伪造工具Swaks,工具项目主页为 http://jetmore.org/john/code/swaks 2.基本用法: swaks --to --from --e ...
- FPGA设计方法检查表
-----------------------摘自<FPGA软件测试与评价技术> 中国电子信息产业发展研究院 | 编著------------------------------- 文本格 ...
- python目前安装的包备份
Package Version ------------------------------- ------------------ alembic altgraph 0.14 apistar app ...
- 使用pandas把mysql的数据导入MongoDB。
使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...
- gc overhead limit exceeded内存问题
gc overhead limit exceeded 当出现这种问题的时候一般有两种思路 一.修改idea或者eclipse中的配置文件,将内存调到1024即可 二.在代码中通过system.gc 手 ...
- python 写入execl记录
记录代码中关于写execl的操作 # 创建execl workbook = xlwt.Workbook(encoding='utf8') # 创建样式实例 style = xlwt.XFStyle() ...
- Oracle 中count(1) 、count(*) 和count(列名) 函数的区别
1)count(1)与count(*)比较: 1.如果你的数据表没有主键,那么count(1)比count(*)快2.如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快3. ...