【bzoj2176】Strange string

来源:互联网 发布:js获取post过来的值 编辑:程序博客网 时间:2024/05/16 11:23

Description

给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的SS的字串中最小的(字符串的比较), 则称T为”奇怪的字串”. 你的任务就是找出这个字符串.
Input

读入两行, 第一行为n, 第二行为字符串S.
Output

将”奇怪的字串” T输出输入样例
Sample Input

10

asdfasdfas

Sample Output

asasdfasdf

HINT

数据范围

对于100%的数据, 保证n≤10000000;

给定的字符串中的字符保证在#33 ~ #254之间.

题解
最小表示法
http://blog.csdn.net/zy691357966/article/details/39854359

代码

#include<bits/stdc++.h>#define pa pair<int,int>#define ll long long#define inf 10000005using namespace std;inline int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int n;  unsigned char ch[10001000];int Min_Representation(){    int i=1,j=2,k=0;    while (i<=n&&j<=n&&k<=n)    {        int x=(i+k-1)%n+1,y=(j+k-1)%n+1;        if (ch[x]==ch[y]) k++;        else if (ch[x]>ch[y]) i=i+k+1,k=0;        else j=j+k+1,k=0;        if (i==j) j++;    }    return min(i,j);}int main(){    scanf("%d\n",&n);     fread(ch+1,1,n,stdin);    int ans=Min_Representation();    if (ans==1)fwrite(ch+1,1,n,stdout);    else fwrite(ch+ans,1,n-ans+1,stdout),fwrite(ch+1,1,ans-1,stdout);    puts("");    return 0;}
原创粉丝点击