Glass Beads

UVA719
将循环串SSS展开成两倍大小:S+SS+SS+S,这样线性处理就可以处理所有循环的情况了。对S+SS+SS+S建立一个后缀自动机,让后从初始状态开始走,每次选择字典序最小的道路,走NNN步就得到一个字典序最小的原串了。假设最后走到ppp,那么此时首字符下标即为len(p)−N+1len(p)-N+1len(p)−N+1,即从首字符的位置走了NNN步到ppp。
AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
const int MAXN = 20000;
int n;
char S[2*MAXN], Buffer[MAXN];
struct SAM {
int size, last;
struct Node {
int len, link;
int next[26];
int cnt;
void clear() {
len = link = 0;
memset(next, 0, sizeof(next));
}
} node[MAXN * 2];
void init() {
for (int i = 0; i < size; i++) {
node[i].clear();
}
node[0].link = -1;
size = 1;
last = 0;
}
void insert(char x) {
int ch = x - 'a';
int cur = size++;
node[cur].len = node[last].len + 1;
node[cur].cnt = 1;
int p = last;
while (p != -1 && !node[p].next[ch]) {
node[p].next[ch] = cur;
p = node[p].link;
}
if (p == -1) {
node[cur].link = 0;
}
else {
int q = node[p].next[ch];
if (node[p].len + 1 == node[q].len) {
node[cur].link = q;
}
else {
int clone = size++;
node[clone] = node[q];
node[clone].len = node[p].len + 1;
node[clone].cnt = 0;
while (p != -1 && node[p].next[ch] == q) {
node[p].next[ch] = clone;
p = node[p].link;
}
node[q].link = node[cur].link = clone;
}
}
last = cur;
}
} sam;
int main() {
int T;
scanf("%d", &T);
while (T--) {
sam.init();
scanf("%s", S);
n = strlen(S);
strcpy(Buffer, S);
strcat(S, Buffer);
n = strlen(S);
for (int i = 0; i < n; i++) {
sam.insert(S[i]);
}
int&& Cur = 0;
n /= 2;
//走N步
for (int i = 0; i < n ; ++i) {
for (int j = 0; j < 26; ++j) {
//如果走得通(走最小的)
if (sam.node[Cur].next[j]) {
Cur = sam.node[Cur].next[j];
break;
}
}
}
printf("%d\n", sam.node[Cur].len - n + 1);
}
return 0;
}
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 ...
随机推荐
- sql offset 优化
// let groupSql = ` select id,jd_gcj02ll, wd_gcj02ll from ${tablename_qiye} where id between ${size ...
- AVL tree rotate
AVL tree single rotate /** * Rotate binary tree node with left child. * For AVL trees, this is a sin ...
- PowerShell学习笔记五_模块
PowserShell其他技巧 1. 执行完一个ps1文件后,不回收 有一个场景,在fun.ps1中,仅仅写了一段 Funtion fun([String] input) { } 然后打开PowerS ...
- TODO留学小程序,展开,收起失效
text设置user-select=true后,display: -webkit-box 失效? https://developers.weixin.qq.com/community/develop/ ...
- String intern的理解
String s1 = "Programming"; s1在栈内存中,Programming在常量池中. String s2 = new String("Progra ...
- 实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js)
实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js) 1. 效果图: 2.实现分页数据请求的思路: 分页 ...
- HELM的使用
一.helm的主要功能 1.创建新的chart 2.chart打包成tgz格式 3.上传chart到chart仓库或从仓库中下载chart 4.在kubernetes集群中安装或卸载chart 5.管 ...
- springboot+mybais配置多数据源(分包实现)
一.分包方式实现: 1.在application.properties中配置两个数据库: #druid连接池 #dataSoureOne(这里是我本地的数据源) spring.datasource.o ...
- 2357. 使数组中所有元素都等于零 (Easy)
问题描述 2357. 使数组中所有元素都等于零 (Easy) 给你一个非负整数数组 nums .在一步操作中,你必须: 选出一个正整数 x , x 需要小于或等于 nums 中 最小 的 非零 元素. ...
- S-HR查询用户组织范围
SELECT org.FNumber FNumber,org.FName_L2 orgName FROM T_PM_OrgRange orgRange LEFT JOIN T_ORG_admin or ...