1231 Problem X- minimum string represents - Getting problems - to achieve string processing -C ++

Problem X: Minimum string representation

Time limit: 1 Sec memory limit: 32 MB
submitted: 129 solution: 36

Title Description

The string of a length len of a circle, then any character as a starting point, will have a length len of the string, the string representation is the smallest of the minimum order that all the strings in the dictionary.
Such as string alabala, after it a circle, formed in accordance with the above rules the following new string:
labalaa
abalaal
balaala
alaalab
laalaba
aalabal
in that all seven string is lexicographically smallest aalabal, its first the position of a letter in the original string is 6. (Position starting from 0 count)
now given you a string, you find out the location of its first letter represents the smallest in the original string. If the minimum string representation of a plurality, then the output of the first letter in the minimum position of the original string.

Entry

The first line of the input is an integer t, t expressed set of test data.
Next t lines of a first input integer l (5 <= l <= 100000), indicates the length of the string, and then enter a string that represents the original string. String contains only lowercase letters.

Export

For each position of the first letter of the input and output in the original string represented by the smallest of the original string.

Sample input  Copy

2 
6 baabaa 
7 alabala

Sample output  Copy

1
6

Code

prompt:

This question is you understand yet? It took me some time to understand this question, and finally found:

  • Between strings may be larger than the size (ABCE and abd: abce, the end of the third alignment abd>)
  • Stitching together two identical strings can play a traverse cycle time of the array (for example: ABCD and abcd = abcdabcd)
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

int main(){
    
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin >>s;

        int len;
        len=(int)s.size();

        s+=s;//就循环了

        string sans;
        sans=s.substr(0,len);

        int ans;
        ans=0;

        char cmin  ;
        cmin='z';
        for(int i = 0;i < len;i++){
            cmin=min(cmin,s[i]);
        }
        for(int i = 1;i < len;i++){
            if(s[i]==cmin){
                if(sans>s.substr(i,len)){
                    sans=s.substr(i,len);
                    ans=i;
                }

            }
        }
        cout<<ans<<endl;

    }
    return 0;
}

 

Published 20 original articles · won praise 0 · Views 115

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/104736735