2019牛客暑期多校训练营(第七场)A String

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 \leq 300)T (T≤300). 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.

输入

4
0
0001
0010
111011110

输出

0
0001
001 0
111 01111 0

题目给的数据很小,所以不要往难处想,直接暴力即可:

#include 
using namespace std;

bool judge(string s){
    int n=s.length();
    for(int i=1;i<n;i++){
        for(int j=0;j<n;j++){
            if(s[j]<s[(i+j)%n]) break;
            if(s[j]>s[(i+j)%n]) return false;
        }
    }
    return true;
}

int main()
{
int t,n;
string s;
scanf("%d",&t);
while(t--){
    cin>>s;
    n=s.length();
    for(int i=0;i<n;i++){
        for(int j=n-i;j>0;j--){
            string ss=s.substr(i,j);
            if(judge(ss)){
                cout<<ss<<" ";
                i+=j-1;//注意是加j-1,因为break之后i还要加1
                break;
            }
        }
     }
    cout<<endl;
  }
}

你可能感兴趣的:(字符串,牛客,暴力)