Glass Beads

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 5545
64-bit integer IO format: %lld      Java class name: Main

 

Once upon a time there was a famous actress. As you may expect, she played mostly Antique Comedies most of all. All the people loved her. But she was not interested in the crowds. Her big hobby were beads of any kind. Many bead makers were working for her and they manufactured new necklaces and bracelets every day. One day she called her mainInspector of Bead Makers (IBM) and told him she wanted a very long and special necklace.

The necklace should be made of glass beads of different sizes connected to each other but without any thread running through the beads, so that means the beads can be disconnected at any point. The actress chose the succession of beads she wants to have and the IBM promised to make the necklace. But then he realized a problem. The joint between two neighbouring beads is not very robust so it is possible that the necklace will get torn by its own weight. The situation becomes even worse when the necklace is disjoined. Moreover, the point of disconnection is very important. If there are small beads at the beginning, the possibility of tearing is much higher than if there were large beads. IBM wants to test the robustness of a necklace so he needs a program that will be able to determine the worst possible point of disjoining the beads.

The description of the necklace is a string  specifying sizes of the particular beads, where the last character am is considered to precede character a1 in circular fashion.

The disjoint point i is said to be worse than the disjoint point j if and only if the string  is lexicografically smaller than the string . String  is lexicografically smaller than the string  if and only if there exists an integer , so that aj=bj, for each  and ai < bi.

 

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line containing necklace description. Maximal length of each description is 10000 characters. Each bead is represented by a lower-case character of the english alphabet (a-z), where .

 

Output

For each case, print exactly one line containing only one integer - number of the bead which is the first at the worst possible disjoining, i.e. such i, that the string A[i] is lexicographically smallest among all the n possible disjoinings of a necklace. If there are more than one solution, print the one with the lowest i.

 

Sample Input

4
helloworld
amandamanda
dontcallmebfu
aaabaaa

Sample Output

10
11
6
5

Source

 
解题;耍耍SAM,看不懂构建的原理,奶奶个熊
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct SAM {
struct node {
int son[],f,len;
void init() {
f = -;
len = ;
memset(son,-,sizeof son);
}
} sn[maxn<<];
int tot,last;
void init() {
tot = last = ;
sn[tot++].init();
}
int newnode() {
sn[tot].init();
return tot++;
}
void extend(int c) {
int np = newnode(),p = last;
sn[np].len = sn[last].len + ;
while(p != - && sn[p].son[c] == -) {
sn[p].son[c] = np;
p = sn[p].f;
}
if(p == -) sn[np].f = ;
else {
int q = sn[p].son[c];
if(sn[p].len + == sn[q].len) sn[np].f = q;
else {
int nq = newnode();
sn[nq] = sn[q];
sn[nq].len = sn[p].len + ;
sn[np].f = sn[q].f = nq;
while(p != - && sn[p].son[c] == q) {
sn[p].son[c] = nq;
p = sn[p].f;
}
}
}
last = np;
}
} sam;
char str[maxn];
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
sam.init();
scanf("%s",str);
int len = strlen(str),p = ;
for(int i = ; i < (len<<); ++i) {
sam.extend(str[i%len]-'a');
}
for(int i = ; i < len; ++i) {
for(int j = ; j < ; ++j) {
if(sam.sn[p].son[j] != -) {
p = sam.sn[p].son[j];
break;
}
}
}
printf("%d\n",sam.sn[p].len - len + );
}
return ;
}
/*
4
helloworld
amandamanda
dontcallmebfu
aaabaaa
*/

UVALive 5545 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. POJ 1509 Glass Beads

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

  7. 1509 -- Glass Beads POJ

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

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

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

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

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

随机推荐

  1. FreeMarker 语法 null 的处理

    一.java 代码 @Test public void testFreeMarker() throws Exception { //1.创建一个模板文件 //2.创建一个Configuration对象 ...

  2. spring data JPA 中的多属性排序

    在此介绍我所用的一种方式: 第一步,引包 import org.springframework.data.domain.Sort;import org.springframework.data.dom ...

  3. 工具-VMWARE技巧-桥接连外网-WIN7

    使用虚拟机wmware如何连接宿主主机 最简单的方法,使用直接连接主机的模式,然后把宿主机的IP更改为和虚拟机同一个网段的机器就行 但是既要上网,有要连接虚拟机,就需要使用桥接,在编辑->虚拟网 ...

  4. Android自己定义对话框实现QQ退出界面

    效果 首先看下qq的效果图,点击菜单button后点退出就会出现如图的对话框. 从上图能够看出,该对话框有一个圆角,以及标题,提示信息,两个button,button颜色是白色,button点击后背景 ...

  5. Window7幻灯片字体显示混乱,难道真的是病毒么

    这个问题有几天了.就是在其它人的PowerPoint2010做的ppt文件.发到这台有问题的电脑上(PowerPoint2007)就会显示全然不一样.例如以下所看到的. watermark/2/tex ...

  6. C++数值类型极限值的获取

    C/C++中基本类型的数值极限值一般来说都是与详细平台有关的,在程序设计的过程中为了写出与平台无关的程序则必须通过合理科学的方法去获取各种类型的极值,经常使用的获取方法有两种:一种是传统的C语言所採用 ...

  7. json 与其他数据 格式转换及json学习新得

    jsonobject   var a={"a","A"}   通过json都对象能很轻松的操作json数据 jsonString     var a=" ...

  8. XTU1202:逆序数

    题目描写叙述 有n张卡片,分别标有数字1~n. 有一天Silence把他们按某种序列排好.然后从第一张開始取出一张,再拿一张放到最后面.再取出一张,再拿出一张放到最后面...知道n张卡片所有取走. 把 ...

  9. 0x15 KMP

    这个算法本身就不难. poj1961 #include<cstdio> #include<iostream> #include<cstring> #include& ...

  10. mysqli简介

    mysqli简介 PHP MySQLi 简介 PHP MySQLi = PHP MySQL Improved! MySQLi 函数允许您访问 MySQL 数据库服务器. 注释:MySQLi 扩展被设计 ...