String

题目链接:https://ac.nowcoder.com/acm/contest/887/A

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

题目描述

A string is perfect if it has the smallest lexicographical ordering among its cyclic rotations.
For example: "0101" is perfect as it is the smallest string among ("0101", "1010", "0101", "1010").

Given a 01 string, you need to split it into the least parts and all parts are perfect.

输入描述:

The first line of the input gives the number of test cases, T (T≤300)T\ (T \leq 300)T (T300).  test cases follow.

For each test case, the only line contains one non-empty 01 string. The length of string is not exceed 200.

输出描述:

For each test case, output one string separated by a space.
示例1

输入

复制
4
0
0001
0010
111011110

输出

复制
0
0001
001 0
111 01111 0
题目大意:把一个串拆分成几个串,保证拆分后的串都是字典序最小的串
思路:从后往前暴力枚举串,如果串是一个字典序最小的串,那么改变起点,一直枚举下去,就是答案了
看代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int Get_min(string s)//得到串的最小表示所在的下标
{
    int n=s.size();int i=0,j=1,k=0;
    while(i<n&&j<n&&k<n)
    {
        int t=s[(i+k)%n]-s[(j+k)%n];
        if(!t) k++;
        else
        {
            if(t<0) j+=k+1;
            else i+=k+1;
            if(i==j) j++;
            k=0;
        }
    }
    int pos=min(i,j);
    return pos==0;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        string s;cin>>s;
        int len=s.size();
        int l=0,r=len;
        while(true)
        {
            for(int i=r;i>=0;i--)//枚举终点
            {
                string t=s.substr(l,i-l);
                if(Get_min(t))
                {
                    for(int j=l;j<i;j++) cout<<s[j];cout<<" ";
                    l=i;break;
                }
            }
            if(l==r) break;
        }
        cout<<endl;
    }
    return 0;
}

 

posted @ 2019-08-09 15:13  执||念  阅读(179)  评论(0)    收藏  举报