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

  1. 4
  2. helloworld
  3. amandamanda
  4. dontcallmebfu
  5. aaabaaa

Sample Output

  1. 10
  2. 11
  3. 6
  4. 5

题意:

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

方法:

1,最小表示法。

2,后缀自动机。

后缀自动机求最小表示:

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

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

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<memory>
  7. using namespace std;
  8. const int maxn=;
  9. char chr[maxn];int L;
  10. struct SAM
  11. {
  12. int slink[maxn],tran[maxn][],maxlen[maxn],root,sz,Last;
  13. void init()
  14. {
  15. root=;sz=;Last=;maxlen[]=slink[]=;
  16. memset(tran[],,sizeof(tran[]));
  17. }
  18. void add(int x)
  19. {
  20. int np=++sz,p=Last;Last=np;
  21. maxlen[np]=maxlen[p]+;
  22. memset(tran[np],,sizeof(tran[np]));
  23. while(p&&!tran[p][x]) tran[p][x]=np,p=slink[p];
  24. if(!p) slink[np]=;
  25. else {
  26. int q=tran[p][x];
  27. if(maxlen[q]==maxlen[p]+) slink[np]=q;
  28. else {
  29. int nq=++sz;
  30. memcpy(tran[nq],tran[q],sizeof(tran[q]));
  31. slink[nq]=slink[q],slink[np]=slink[q]=nq;
  32. maxlen[nq]=maxlen[p]+;
  33. while(p&&tran[p][x]==q) tran[p][x]=nq,p=slink[p];
  34. }
  35. }
  36. }
  37. void solve()
  38. {
  39. int Now=root;
  40. for(int i=;i<L;i++){
  41. for(int j=;j<;j++){
  42. if(tran[Now][j]) {
  43. Now=tran[Now][j];break;
  44. }
  45. }
  46. }
  47. printf("%d\n",maxlen[Now]-L+);
  48. }
  49. };
  50. SAM S;
  51. int main()
  52. {
  53. int i,j,n;
  54. while(~scanf("%d",&n)){
  55. while(n--){
  56. S.init();
  57. scanf("%s",chr);
  58. L=strlen(chr);
  59. for(i=;i<L;i++) S.add(chr[i]-'a');
  60. for(i=;i<L;i++) S.add(chr[i]-'a');
  61. S.solve();
  62. }
  63. }
  64. return ;
  65. }

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. python基础知识(元组)

    元组 不能更改内容 元组 (元素1,元素2) 元组的创建和删除 使用赋值运算符直接创建元组 元组名 = (元素1,元素2........) 只创建一个元素的元组    元组名 = (元素1,) 创建空 ...

  2. 云计算共享组件--消息队列rabbitmq(3)

    一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 消息传 ...

  3. TensorFlow实战第八课(卷积神经网络CNN)

    首先我们来简单的了解一下什么是卷积神经网路(Convolutional Neural Network) 卷积神经网络是近些年逐步兴起的一种人工神经网络结构, 因为利用卷积神经网络在图像和语音识别方面能 ...

  4. 贡献python prim多源最短路搜索算法 numba加速方法的demo和总结

    1.测试两个算法 #coding:utf-8 import time import numba import numpy as np ''' 使用numba加速总结, (1).在数值计算比如int f ...

  5. DDOS常见攻击类型和防御措施

    DDOS 攻击类型: SYN Flood 攻击 ACK Flood 攻击 UDP Flood 攻击 ICMP Flood 攻击 Connection Flood 攻击 HTTP Get 攻击 UDP ...

  6. HDU 1260 Tickets (动态规划)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. Spring IOC 和Aspectj AOP

    1.Aspectj AOP 是一套独立的AOP 解决方案,不仅限于java应用,不依赖其他方案,属于编译时增强,有自己单独的编译器.Spring AOP 是基于Spring 容器的的AOP解决方式,属 ...

  8. Ubuntu基本操作(博主想上传图片给服务器的一些命令)

    1.将当前目录下的文件移动至指定文件夹,这里用移动至网站的根目录做示范 sudo mv bamboo.jpg /val/www/html mv bamboo.jpg /val/www/html 2.进 ...

  9. python_0基础开始_day08

    第八节 1,文件操作 文件操作目的: 持久化,永久存储 (数据库之前 -- 文件操作就是代替数据库) 读 1,找到文件位 2,双击打开 3,进行一些操作 4,关闭文件 open() 打开,通过pyth ...

  10. Codeforces 1201E2. Knightmare (hard)

    传送门 看到棋盘先黑白染色冷静一下 然后分析发现,如果初始时两只马在同色的格子,那么一定是后手吃先手 反之一定是先手吃后手 所以分类讨论一下,如果初始在同色的格子,并且后手到达终点的步数更少,那么后手 ...