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 main Inspector 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 A = a1a2 ... am 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 aiai+1 ... ana1 ... ai-1 is lexicografically smaller than the string ajaj+1 ... ana1 ... aj-1. String a1a2 ... an is lexicografically smaller than the string b1b2 ... bn if and only if there exists an integer i, i <= n, so that aj=bj, for each j, 1 <= j < i 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 a < b ... z.

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

题意:

给一个字符串(环),求出其最小表示的头位置下标。比如aaaba(pos)aa中第5个a为头。

方法:

1,最小表示法。

2,后缀自动机。

后缀自动机求最小表示:

把字符串S复制SS,然后构建后缀自动机。因为后缀自动机得到的是任意区间的字符串[i,j] 。然后跟着root一直找可以取到的最小即可。长度为|S|。

(ps:这里可以类比字典树trie树的排序功能可以看一下)。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<memory>
using namespace std;
const int maxn=;
char chr[maxn];int L;
struct SAM
{
int slink[maxn],tran[maxn][],maxlen[maxn],root,sz,Last;
void init()
{
root=;sz=;Last=;maxlen[]=slink[]=;
memset(tran[],,sizeof(tran[]));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;
maxlen[np]=maxlen[p]+;
memset(tran[np],,sizeof(tran[np]));
while(p&&!tran[p][x]) tran[p][x]=np,p=slink[p];
if(!p) slink[np]=;
else {
int q=tran[p][x];
if(maxlen[q]==maxlen[p]+) slink[np]=q;
else {
int nq=++sz;
memcpy(tran[nq],tran[q],sizeof(tran[q]));
slink[nq]=slink[q],slink[np]=slink[q]=nq;
maxlen[nq]=maxlen[p]+;
while(p&&tran[p][x]==q) tran[p][x]=nq,p=slink[p];
}
}
}
void solve()
{
int Now=root;
for(int i=;i<L;i++){
for(int j=;j<;j++){
if(tran[Now][j]) {
Now=tran[Now][j];break;
}
}
}
printf("%d\n",maxlen[Now]-L+);
}
};
SAM S;
int main()
{
int i,j,n;
while(~scanf("%d",&n)){
while(n--){
S.init();
scanf("%s",chr);
L=strlen(chr);
for(i=;i<L;i++) S.add(chr[i]-'a');
for(i=;i<L;i++) S.add(chr[i]-'a');
S.solve();
}
}
return ;
}

POJ1059Glass Beads的更多相关文章

  1. HDU 1817Necklace of Beads(置换+Polya计数)

    Necklace of Beads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. 【USACO】beads

    题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...

  3. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  4. 数学计数原理(Pólya):POJ 1286 Necklace of Beads

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7763   Accepted: 3247 ...

  5. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  6. POJ 1286 Necklace of Beads(项链的珠子)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7874   Accepted: 3290 ...

  7. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

  8. POJ1509 Glass Beads

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

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

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

随机推荐

  1. R数据分析(一)

    R语言特点: 主要用于统计分析.图表显示.   属于解释型语言.支持模块化编程. 应用:数据科学.统计计算.机器学习   学习方法: 做笔记,记重点或者心得 手动实践,加深理解 坚持练习,利用身边数据 ...

  2. docker-compose 部署elk+解决时间不对导致kibana找不到logstash定义的index + docker-compose安装

    1.拉代码 git clone https://github.com/deviantony/docker-elk.git 2.docker-compose配置文件 [root@host7 docker ...

  3. CMMI将能力成熟度分为5个级别

    CMMI将能力成熟度分为5个级别(初始级,已管理级,已定义级,量化管理级,优化级) . 初始级 此时软件过程是无序的,有时甚至是混乱的,对过程几乎没有定义,成功取决于个人努力.管理是反应式的. .可管 ...

  4. 【转贴】龙芯内核发展策略 已经支持k8s

    龙芯内核发展策略 时间:2019-06-27 15:48  来源:未知   作者:龙芯中科   点击:1002次 http://www.loongson.cn/m/view.php?aid=1118 ...

  5. 有关最短路上的第k小/大值的总结

    1.USACO08JAN  Telephone Lines 题面 由于问的是最大值最小,所以二分加验证就好了 比较显然的,题干问的是第k+1长的路最短: 那么二分答案是正确的方向: 但是怎么验证? 我 ...

  6. Yii2.0中使用Union查询,并使用join,支持分页

    $query1 = Class1::find()->where($where); $query2 = Class1::find()->alias('a')->join('left j ...

  7. Jenkins的安装配置及使用

    一.以Jenkins在tomcat容器里运行的方式,jenkins的安装及安装时所涉及的JDK和tomcat的配置 1.首先下载tomcat, 2.下载Jenkins.war包,将war包放在tomc ...

  8. Codeforces 1196E. Connected Component on a Chessboard

    传送门 注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况 不妨假设需要的白色格子比黑色格子少 那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周 ...

  9. leecode100热题 HOT 100

    # 题名 题解 通过率 难度 出现频率     1 两数之和       46.5% 简单     2 两数相加       35.5% 中等     3 无重复字符的最长子串       31.1% ...

  10. 命名空间System.IO

    基本介绍:System.IO 命名空间提供读写文件和数据流的类型.基本文件和目录支持的类型. 原文:http://blog.sina.com.cn/s/blog_48a45b950100erhz.ht ...