POJ 1509 Glass Beads

POJ 1509 Glass Beads

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

后缀自己主动机的简单运用….

Glass Beads
Time Limit: 3000MS   Memory Limit: 10000K
Total Submissions: 2352   Accepted: 1375

Description

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int CHAR=26,maxn=50000;

struct SAM_Node
{
    SAM_Node *fa,*next[CHAR];
    int len,id,pos;
    SAM_Node(){}
    SAM_Node(int _len)
    {
        fa=0; len=_len;
        memset(next,0,sizeof(next));
    }
};

SAM_Node SAM_node[maxn],*SAM_root,*SAM_last;
int SAM_size;

SAM_Node *newSAM_Node(int len)
{
    SAM_node[SAM_size]=SAM_Node(len);
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
}

SAM_Node *newSAM_Node(SAM_Node *p)
{
    SAM_node[SAM_size]=*p;
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
}

void SAM_init()
{
    SAM_size=0;
    SAM_root=SAM_last=newSAM_Node(0);
    SAM_node[0].pos=0;
}

void SAM_add(int x,int len)
{
    SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
    np->pos=len; SAM_last=np;
    for(;p&&!p->next[x];p=p->fa) p->next[x]=np;
    if(!p)
    {
        np->fa=SAM_root; return ;
    }
    SAM_Node *q=p->next[x];
    if(p->len+1==q->len)
    {
        np->fa=q; return;
    }
    SAM_Node *nq=newSAM_Node(q);
    nq->len=p->len+1;
    q->fa=nq; np->fa=nq;
    for(;p&&p->next[x]==q;p=p->fa)
        p->next[x]=nq;
}

void SAM_build(char * s)
{
    SAM_init();
    int len=strlen(s);
    for(int i=0;i<len;i++)
        SAM_add(s[i]-'a',i+1);
}

char str[maxn];
int the_last=-1;

void dfs(SAM_Node* head,int n)
{
    if(n==0)
    {
        the_last=head->pos;
        return ;
    }
    for(int i=0;i<26;i++)
    {
        if(head->next[i])
        {
            dfs(head->next[i],n-1);
            return ;
        }
    }
}

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%s",str);
        int n=strlen(str);
        for(int i=0;i<n;i++)
            str[i+n]=str[i];
        str[2*n]=0;
        SAM_build(str);
        SAM_Node *temp=SAM_root;
        n=strlen(str);
        dfs(temp,n/2);
        printf("%d\n",the_last+1-n/2);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118888.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 怎么新建pytest的ini文件_qt读写配置文件

    怎么新建pytest的ini文件_qt读写配置文件前言pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行查看pytest.ini的配置选项pytest-h找到以下

    2022年8月6日
    7
  • offsetof实现_set off等于

    offsetof实现_set off等于使用实例:typedefstruct{constAVClass*class;char*expr_str;AVExpr*expr;doublevar_values[VAR_VARS_NB];enumAVMediaTypetype;}SetPTSContext;#defineOFFSET(x)offsetof(SetPTSContext,x)#defineFLAGSAV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM

    2022年8月22日
    6
  • Java设计模式——策略模式[通俗易懂]

    Java设计模式——策略模式[通俗易懂]策略模式1.策略模式简介策略模式:策略模式是一种行为型模式,它将对象和行为分开,将行为定义为一个行为接口和具体行为的实现。策略模式最大的特点是行为的变化,行为之间可以相互替换。每个if判断都可以理解为就是一个策略。本模式使得算法可独立于使用它的用户而变化2.模式结构策略模式包含如下角色:Strategy:抽象策略类:策略是一个接口,该接口定义若干个算法标识,即定义了若干个抽象方法(如下图的algorithm())Context:环境类/上下文类:上下文是依赖于接口的类(

    2025年10月15日
    3
  • 大整数乘法C

    大整数乘法C大整数乘法C语言实现希望能帮到你们#include&lt;iostream&gt;#include&lt;cstdio&gt;#include&lt;algorithm&gt;#include&lt;cstring&gt;#defineMAX210usingnamespacestd;intmain(intargc,charconst*argv[]){…

    2022年5月5日
    53
  • 通用目标检测_ug目标体完全处于工具体内部

    通用目标检测_ug目标体完全处于工具体内部睿智的目标检测-番外篇——数据增强在目标检测中的应用学习前言数据增强做了什么学习前言数据增强是非常重要的提高目标检测算法鲁棒性的手段,学习一下对身体有好处!数据增强做了什么…

    2022年10月10日
    2
  • 查看三种MySQL字符集的方法

    查看三种MySQL字符集的方法

    2021年10月12日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号