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. 系统 --- Linux系统环境搭建

    Linux命令介绍 软硬链接 作用:建立连接文件,linux下的连接文件类似于windows下的快捷方式 分类: 软链接:软链接不占用磁盘空间,源文件删除则软链接失效 硬链接:硬链接只能链接不同文件, ...

  2. WIN10远程协助无法控制的解决方法

    这个问题比较常见小编整理的解决方法如下: 方法一:用QQ远程协助对方电脑,需要QQ告诉对方右键单击计算机(这台电脑)点管理打开计算机管理界面选择本地用户和组,再选择用户,右侧会出现所有的本地用户,包括 ...

  3. 【Linux开发】linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现

    linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一.结构体struct file和struct inode 在之前写的函数,全部是定义了一些零散的全局变量.有没有办法整合 ...

  4. C++学习笔记-模板

    模板把函数或类要处理的数据类型参数化,表现为参数的多态性,称为类属.模板用于表达逻辑结构相同,但具体数据元素类型不同的数据对象的通用行为. 什么是模板 类属--类型参数化,又称参数模板 使得程序(算法 ...

  5. 使用Python过程中遇到的一些坑及其解决方法(持续更新)

    1.列表不能直接赋值 nums1 = nums2 x nums1[:] = nums2 正确 2.返回列表某一元素的值可以使用index函数 aList = [123, 'xyz', 'runoob' ...

  6. PTA(Advanced Level)1046.Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  7. 常用PostgreSQL HA(高可用)工具收集

    PostgreSQL HA Collect: 1.pgpool 2.Pacemaker + Corosync 3.ecox 4.Patroni: A Template for PostgreSQL H ...

  8. 阿里云服务器挖矿脚本bioset攻击解决

    1.问题出现 一大早刚起床,阿里云就给我发了一条短信,提醒我服务器出现紧急安全事件:挖矿程序 阿里云“贴心”地提供了解决方法,不过需要购买企业版的安全服务,本着能自己动手就不花钱原则自己搞了起来 于是 ...

  9. Spring(十二)--Spring AspectJ

    Spring AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. As ...

  10. Java中this与super的区别

    this与super关键字在java中构造函数中的应用: ** super()函数 ** super()函数在子类构造函数中调用父类的构造函数时使用,而且必须要在构造函数的第一行,例如: class ...