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. SSH框架整合截图总结(三)

    联系人信息查询1 点击 联系人信息查询 超链接时候,到查询页面 (1)在查询页面中,选择客户,根据客户进行查询 下拉表框显示所有客户  可以根据所属的客户进行联系人查询  2 在查询页面中,输入值,提 ...

  2. [React] How to use a setState Updater Function with a Reducer Pattern

    In this lesson we'll walk through setting up an updater function that can receive an action argument ...

  3. TeamTalk Android代码分析(业务流程篇)---消息发送和接收的整体逻辑说明

    第一次纪录东西,也没有特别的顺序,想到哪里就随手画了一下,后续会继续整理- 6.2消息页面动作流程 6.2.1 消息页面初始化的总体思路 1.页面数据的填充更新直接由页面主线程从本地数据库请求 2.数 ...

  4. Android广播机制分析

    1.1. 广播简单介绍         Android 广播与生活中的广播概念不同,它是指系统中产生事件后的通知. Android 广播不关心接收者是否收到处理或者怎样处理广播,能够说是一种单向的通知 ...

  5. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  6. hdoj--2120--Ice_cream's world I(并查集判断环)

    Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. Java-MyBatis-杂项: MyBatis 中 in 的用法2

    ylbtech-Java-MyBatis-杂项: MyBatis 中 in 的用法2 1.返回顶部 1. 一.简介 在SQL语法中如果我们想使用in的话直接可以像如下一样使用: select * fr ...

  8. c:forTokens标签delims截取字符

    转自:https://blog.csdn.net/love398146779/article/details/83853958 两个name要相同,在里边内容为空的时候才会全显示. <logic ...

  9. POJ 3180 Tarjan

    题意:找强连通中点数大于2的强连通分量个数 思路:Tarjan // By SiriusRen #include <cstdio> #include <algorithm> u ...

  10. Spark RDD概念学习系列之什么是Pair RDD

    不多说,直接上干货! 什么是Pair RDD (1)包含键值对类型的RDD被称作Pair RDD. (2)Pair RDD通常用来进行聚合计算. (3)Pair RDD通常由普通RDD做ETL转换而来 ...