[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 ...
随机推荐
- halcon发布
1: halcon发布 : 在MFC程序中 添加 #include "include/halcon/cpp/HalconCpp.h"using namespace Halcon;# ...
- Redis简介——(一)
1.关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库,最终会将数据持久化到磁盘上,而nosql数据 库是基于特殊的结构,并将数据存储到内存的数据库.从性能上而言,nosql数据库 ...
- vim加密文件
一.加密文件内容 vim gt-1.sh 输入:X 注意是大写的X 输入密码 然后,保存 再次访问,需要输入密码 如果输入密码错误,内容显示为乱码 用cat或more查看文件内容,显示为乱码:用vi重 ...
- 24 The Go image package go图片包:图片包的基本原理
The Go image package go图片包:图片包的基本原理 21 September 2011 Introduction The image and image/color packag ...
- 第一个Django项目:HelloWorld
OS:Windows家庭中文版, Python:3.6.3,Django:2.0.3 在前一篇文章中,Django已经顺利安装到了Python中,下面,开发第一个Python项目吧! 1.django ...
- Python_oldboy_自动化运维之路_全栈考试(五)
1.执行 Python 脚本的两种方式 [root@localhost tmp]# cat a.py #!/usr/bin/python # -*- coding: UTF-8 -*- print & ...
- windows下同时安装python2和python3
之前学习的时候使用2.7比较多. 想练习3.7的时候,两个版本兼容的问题,苦恼了几天. 查了一下资料,发现了一个很好的方法.记录一下,也做一个分享. 本篇内容主要讲一下,在同一台电脑上如何同时安装Py ...
- HDU 1507 Uncle Tom's Inherited Land(最大匹配+分奇偶部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 题目大意:给你一张n*m大小的图,可以将白色正方形凑成1*2的长方形,问你最多可以凑出几块,并输 ...
- KMP模板及总结
KMP是一种字符串匹配算法,它在时间复杂度上较暴力匹配算法由很大的优势.比如我要找字符串S中是否存在子串P,如果暴力匹配的话,则时间复杂度为O(n*m),而kmp算法时间复杂度为O(n+m). 这里我 ...
- 编程六月定律 | 外刊IT评论网
编程六月定律 上周,我被迫对一个很老的项目做一些修改.麻烦是,当开始着手时,我真的记不清这个项目究竟有多老了. 这实际上是我使用Codeigniter实现的第一个MVC项目.打开项目文件后,很多东西都 ...