字符串的最小表示法

链接:https://ac.nowcoder.com/acm/contest/328/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Rabbit得到了一个字符串,她的好朋友xxx可以给这个字符串施加一次魔法。

魔法可以选择字符串的任一位置,并将该位置后面的所有字符水平拼接到串首。

例如:对于字符串abcde,可以通过施加魔法得到cdeab。

如果xxx通过施加魔法将字符串的字典序变得严格比之前的小,那么他将拿走这一字符串。

Rabbit想知道自己的字符串会不会被xxx拿走。

输入描述:

第一行一个整数n,表示字符串的长度。

接下来一行一个长度为n的只由小写字母组成的字符串。

输出描述:

如果Rabbit的字符串会被xxx拿走,输出“YES”。
否则输出“NO”。
(不输出引号)

示例1

输入

复制

5
cdeab

输出

复制

YES

说明

xxx可以把e之后的部分“ab”放到串首,得到abcde,字典序比cdeab小,故将拿走字符串。

示例2

输入

复制

5
abcde

输出

复制

NO

备注:

1≤n≤100000

字典序的说明:https://en.wikipedia.org/wiki/Alphabetical_order
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=63;
const int N=1e6+5;
char s[N],s1[N];

int min_find(char *s,int len){
    int i=0,j=1,k=0;
    while(i0) i+=k+1;      // s[i+k] >  s[j+k] 意味着从 i - i+k 没有最小前缀
            else j+=k+1;         // s[i+k] <  s[j+k] 意味着从 j - j+k 没有最小前缀
            if(i==j) j++;        // i == j (j向前移,与初始化一样)
            k=0;                 // 从新找,直到 k = len(一个字符串长度)
        }
    }
    return min(i,j);
}

int main(){
    int n;
    scanf("%d",&n);
    scanf(" %s",s);
    int h=min_find(s,n);
    int p=0;
    for(int i=h;i

Hidden Password


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" Geits hide his password. Billy chooses a string S composed of small Latin letters with length L. Then he makes all L-1 one-letter left cyclic shifts of the string and takes as a password one prefix of the lexicographically first of the obtained strings (including S). For example let consider the string alabala. The cyclic one-letter left shifts (including the initial string) are:

alabala
labalaa
abalaal
balaala
alaalab
laalaba
aalabal

and lexicographically first of them is the string aalabal. The first letter of this string is in position 6 in the initial string (the positions in the string are counted from 0).

Write a program that for given string S finds the start position of the smallest lexicographically one-letter left cyclic shift of this string. If the smallest lexicographically left shift appears more than once then the program have to output the smallest initial position.

Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each of the following T lines will describe one test case - first the length L of the string (5 <= L <= 100000) and then, separated by one space, the string S itself.

The output file have to contain exactly T lines with a single number each - the initial position found by your program.


Sample Input

2
6 baabaa
7 alabala


Sample Output

1
6


Source: Southeastern Europe 2003

Submit    Status

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=63;
const int N=1e6+5;
char s[N];
int min_find(char *s,int len){
    int i=0,j=1,k=0;
    while(i0) i+=k+1;
            else j+=k+1;
            if(i==j) j++;
            k=0;
        }
    }
    return min(i,j);
}
int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf(" %s",s);
        printf("%d\n",min_find(s,n));
    }
    return 0;
}

 

你可能感兴趣的:(字符串)