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. 调用百度api的原理流程

    1.为了实现酒店地址的定位 2.使用可视化便捷的百度地图API生成器:设置公司的地址和地图等级 3.设置地图的滚轮.缩放功能 4.获取代码,拷贝到html页面中 5.申请秘钥,在html中引用地图AP ...

  2. mariadb数据库集群

    1.主从架构: 每个从节点需要一个dump线程连接主节点 异步:效率高,安全性低,有延迟 同步:效率低,安全性高,无延迟 主:可读可写,(dump thread) 从:可读不可写 (sql threa ...

  3. [转帖]浙江移动容器云在ARM服务器的实践

    浙江移动容器云在ARM服务器的实践 2019-07-11 22:27 中文社区 分类:Kubernetes实践分享/开发实战 阅读(427) 评论(0)  国产ARM服务器上面跑K8S集群.. 貌似浪 ...

  4. 【Python】【demo实验3】【显示素数,显示两个数范围内的所有素数】

    打印两个整数之间的所有素数: (使用平方根来判断  是否应停止验证该数值是否为素数) for i in range(956253526252,9956253526252): k = 1 if i == ...

  5. plpython 中文分词Windows 版

    windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...

  6. python中super函数的参考

    https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ http://wiki.jikexueyuan.com/proj ...

  7. Python 入门 之 面向对象的三大特性(封装 / 继承 / 多态)

    Python 入门 之 面向对象的三大特性(封装 / 继承 / 多态) 1.面向对象的三大特性: (1)继承 ​ 继承是一种创建新类的方式,在Python中,新建的类可以继承一个或多个父类,父类又可以 ...

  8. 使用zookeeper报错 stat is not executed because it is not in the whitelist. envi is not executed because it is not in the whitelist.

    在使用四字命令或者zk ui界面查看zookeeper集群时,出现如下提示: stat is not executed because it is not in the whitelist. envi ...

  9. 关于记录log日志的几种方法

    最近在记录日志的时候总结了几种方式: 1.使用log4j2记录 2.使用log4j记录 3.使用logback配置,记录前使用 private  final Logger logger = Logge ...

  10. O018、理解 Keystone 核心概念

    参考https://www.cnblogs.com/CloudMan6/p/5365474.html   作为OpenStack的基础支持服务,Keystone做了下面几件事情:       1.管理 ...