zoukankan      html  css  js  c++  java
  • Poj 1509

    Glass Beads
    Time Limit: 3000MS   Memory Limit: 10000K
    Total Submissions: 2827   Accepted: 1634

    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

    大意:
    给出多组数据,每组数据包含一个字符串,求出这个字符串的同构串中字典序最小的串,输出开始的位置.

    分析:
    后缀自动机的简单应用:

    后缀自动机的性质就是从初始态走到任意一个节点都是当前字符串的字串.
    证明:
    后缀自动机能够接受当前的串的所有的后缀.
    在某个时刻,查询的字符串就是整个字符串某个前缀的后缀.所以能够接受.
    根据这个性质

    把两个串接在一起,然后就直接在上面依着最小字典序的边贪心地跑,最后跑到一个终止节点.
    如何计算起始位置呢?
    这个串能够接受的最长的串长,也就是这个点的step.就是从字符串开始到这个点在字符串中的长度.(它曾经是一个结束节点last)

    所以我们只要用它减掉原串长就ok了.

    代码比较简单.
     1 #include<cstdlib>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<iostream>
     6 using namespace std;
     7 const int maxn = (int)1e5, sigma = 26;
     8 struct Sam{
     9     int ch[maxn][sigma],par[maxn];
    10     int stp[maxn];
    11     int sz,last;
    12     void init(){
    13         memset(ch,0,sizeof(ch));
    14         memset(par,0,sizeof(par));
    15         memset(stp,0,sizeof(stp));
    16         sz = last = 1;
    17     }
    18     void extend(int c){
    19         stp[++sz] = stp[last] + 1;
    20         int p = last, np = sz;
    21         for(; !ch[p][c]; p = par[p]) ch[p][c] = np;
    22         if(p == 0) par[np] = 1;
    23         else{
    24             int q = ch[p][c];
    25             if(stp[q] != stp[p] + 1){
    26                 stp[++sz] = stp[p] + 1;
    27                 int nq = sz;
    28                 memcpy(ch[nq],ch[q],sizeof(ch[q]));
    29                 par[nq] = par[q];
    30                 par[q] = par[np] = nq;
    31                 for(; ch[p][c] == q; p = par[p]) ch[p][c] = nq;
    32             }
    33             else par[np] = q;
    34         }
    35         last = np;
    36     }
    37     void build(string &s){
    38         int i;
    39         init();
    40         for(i = 0; i < s.size(); ++i) extend(s[i] - 'a');
    41     }
    42     void vis(int len){
    43         int x = 1,i,j;
    44         for(i = 0; i < len; ++i)
    45             for(j = 0; j < sigma; ++j)
    46                 if(ch[x][j]){ x = ch[x][j]; break; }
    47         cout << stp[x] - len + 1 << endl;
    48     }
    49 }sam;
    50 string s;
    51 int main()
    52 {
    53     freopen("bead.in","r",stdin);
    54     freopen("bead.out","w",stdout);
    55     ios::sync_with_stdio(false);
    56     int n,i;
    57     cin >> n;
    58     for(i = 1; i <= n; ++i){
    59         cin >> s;
    60         s = s + s;
    61         sam.build(s);
    62         sam.vis(s.size() / 2);
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    ovs QOS
    OpenvSwitch端口镜像
    MyCat入门指南
    Mycat跨分片Join
    MyCAT 命令行监控
    MyCat的分片规则
    FreeMarker初探--介绍
    FreeMarker初探--安装FreeMarker
    linux 安装配置zookeeper
    Maven 环境搭建及相应的配置
  • 原文地址:https://www.cnblogs.com/Mr-ren/p/4209532.html
Copyright © 2011-2022 走看看