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的更多相关文章

  1. POJ1509 Glass Beads

    Glass Beads Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4314   Accepted: 2448 Descr ...

  2. POJ1509 Glass Beads(最小表示法 后缀自动机)

    Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4901   Accepted: 2765 Description Once ...

  3. 【POJ1509】Glass Beads

    [POJ1509]Glass Beads [题目描述]给定字符串S,并规定首尾相连成环,求出最小字典序. [输入]输入有多个数据,第一行只包括正整数N,表示有N组数据.每个数据包括一行,输入该字符串. ...

  4. zoj 2006 Glass Beads

    Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006 Time Limit: 2 Seconds     ...

  5. cogs 2123. [HZOI 2015] Glass Beads

    2123. [HZOI 2015] Glass Beads ★★★   输入文件:MinRepresentations.in   输出文件:MinRepresentations.out   简单对比时 ...

  6. UVALive 5545 Glass Beads

    Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...

  7. POJ 1509 Glass Beads

    Description 求字符串的最小循环表示. Sol SAM. 把原串复制一遍,建出SAM,然后每次选最小的一个跑 \(len\) 次,这就是最小循环表示的最后一个节点,然后 \(x-len+1\ ...

  8. 1509 -- Glass Beads POJ

    题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...

  9. 杂项(最小表示法):HZOI 2015 Glass Beads

    [题目描述] 给定长度为n(n<=300000)的循环同构的字符串,定义最小表示为该字符串的字典序最小的同构表示,请输出这个表示. [输入格式] 第一行是串的长度,第二行是字符串. [输出格式] ...

  10. UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)

    题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...

随机推荐

  1. uniapp 中获取微信小程序的原生导航栏高度

    const custom = wx.getMenuButtonBoundingClientRect() // console.log(custom) that.yuansheng= custom.he ...

  2. fix: because the volume group on the selected device also consist of physical volumes on other devices

    because the volume group on the selected device also consist of physical volumes on other devices 目标 ...

  3. c#遍历一个对象的字段信息

    c#遍历对象字段 场景:有一个对象作为导出word段落的数据.每一个字段就代表一个段落,可以对相应段落数据设置样式(字体.颜色.加粗--) 参考文献:(12条消息) C#获取实体类字段信息Proper ...

  4. unity task

    https://blog.csdn.net/weixin_43405845/article/details/105028291

  5. WPF 后台实现按数字键滚动DataGrid 当前选中项

    最近遇到个项目,设备上没有鼠标,界面为全屏的一个DataGrid,需要实现按小键盘的0和1让DataGrid的当前选中行进行上下滚动 起到重要参考的是:   https://blog.csdn.net ...

  6. CF513F2 题解

    题意 传送门 有 \(a+b+1\) 个会动的棋子,在一个大小为 \(n\times m\) 的棋盘上,棋盘上有一些点有障碍.棋子中,有 \(a\) 个红色棋子,\(b\) 个蓝色棋子,和 \(1\) ...

  7. 1238. 循环码排列 (Medium)

    问题描述 1238. 循环码排列 (Medium) 给你两个整数 n 和 start.你的任务是返回任意 (0,1,2,,...,2^n-1) 的排列 p,并且满足: p[0] = start p[i ...

  8. php基础教程(三)

    PHP语法概述 A. 基本语法 即使初次接触PHP的用户也会发现自己对PHP的语法风格并不陌生. 例如:<?php echo "Hello!": ?> 显示结果为&qu ...

  9. python 快速搭建局域网文件服务器 SimpleHTTPServer http.server

    py2: python2 -m SimpleHTTPServer [port] py3:   python3 -m http.server [port] python2请注意大小写. 在Windows ...

  10. 几款Android 应用自动化测试工具

    本文转自:https://blog.csdn.net/hebbely/article/details/78901466 简述: 本文介绍几款流行的 Android应用自动化测试工具. Monkey测试 ...