zoukankan      html  css  js  c++  java
  • hnuun 11544 小明的烦恼——找字符串(求环形字符串的最小最大字典序)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11544&courseid=0

    最小最大表示法:

    求环形字符串的最小最大字典序:

    参考:http://www.cnblogs.com/ziyi--caolu/p/3245132.html

    最小表示法:

       初始时,i=0,j=1,分别以i,j,为起始点顺着i,j,往下比较直到找的str[i+k]!=str[j+k],然后分两种情况考虑:

    1、  str[i+k]>str[j+k],i变成i=i+k+1,j不变,然后继续往下比较。

    2、  str[i+k]<str[j+k],j变成j=j+k+1,i不变,然后继续往下比较。

    直到i或j大于串长,找较小者。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 #pragma comment(linker, "/STACK:102400000,102400000")
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("b.txt", "w", stdout);
    36 #define maxn 1010
    37 #define maxv 1010
    38 #define mod 1000000000
    39 using namespace std;
    40 char str[5000010];
    41 int work(int m)
    42 {
    43     int i,j,l;
    44     i=0; j=1;
    45     while(i<m && j<m)
    46     {
    47         for(l=0;l<m;l++)
    48             if(str[(i+l)%m]!=str[(j+l)%m]) break;
    49         if(l>m) break;
    50         if(str[(i+l)%m] > str[(j+l)%m])
    51             i=i+l+1;
    52         else
    53             j=j+l+1;
    54         if(i==j) j=i+1;
    55     }
    56     if(i<j) return i;
    57     return j;
    58 }
    59 
    60 int main()
    61 {
    62     //freopen("a.txt","r",stdin);
    63     int t;
    64     scanf("%d",&t);
    65     while(t--)
    66     {
    67         scanf("%s",str);
    68        // printf("%s
    ",str);
    69         int l=strlen(str);
    70         printf("%d
    ",work(l));
    71     }
    72     return 0;
    73 }
    View Code

    最大表示法:

     1 int work(int len,char pat[])  //最大表示法
     2 {
     3    //int len = strlen(pat);
     4    int i=0,j=1,k=0;
     5    while(i<len && j<len && k<len)
     6    {
     7        int t = pat[(i+k)%len] - pat[(j+k)%len];
     8        if(!t) k++;
     9        else
    10        {
    11            if(t>0) j = j+k+1;
    12            else i = i+k+1;
    13            if(i == j) j++;
    14            k = 0 ;
    15        }
    16    }
    17    return i<j?i:j;
    18 }
    View Code
  • 相关阅读:
    Codeforces 813F Bipartite Checking 线段树 + 并查集
    Codeforces 263E Rhombus (看题解)
    Codeforces 173E Camping Groups hash
    Codeforces 311C Fetch the Treasure 取模意义下的最短路 (看题解)
    R 培训之 Table
    Docker命令详解
    Celery的实践指南
    Using Celery with Djang
    PostgreSQL
    改时区参考
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4567205.html
Copyright © 2011-2022 走看看