"POJ1509" Glass beads
"Title description" Given the string s, and the provision of the end-to-end ring, to find the most dictionary order.
The input input has more than one data, and the first line contains only positive integer n, which indicates that there are n sets of data. Each data includes one row, and the string is entered.
Output for each data, the output line of data indicates which letter begins with the dictionary order.
"The Solution" violence is a good thing, cough cough
The smallest representation of the fruit of the red fruit.
Q: What is the minimum representation?
A: Look at the topic description Isaac.
Q: How about a notation?
A: Mo-Anxious, look down ha.
- Or start with the violence: compare n strings that begin with different position characters, and then find the one with the smallest dictionary order. Complexity of ... It seems to be O (n^3).
- Consider the process of violence comparison: Set the string strings starting with I to a[i], compare a[i+k] and A[j+k] (0<=k<=n) is equal, find the inequality, determine the position of the element which dictionary is large then the whole dictionary sequence at the beginning of the sequence is larger.
- reconsider the process of violence just now. If we find that i+k and j+k are not equal, it is advisable to set a dictionary order greater than j+k at I+k, then A[i] is definitely not the smallest representation. Second, for A[i+p] and A[j+p], a[j+p] will always be better than a[i+p]. Because two strings are compared at p = k, and the dictionary order at I+k is greater than j+k, the dictionary order of A[j+p] is always less than a[i+p]. So when we can judge a group of such relationships, any two strings that satisfy the same number difference will satisfy the same relationship.
(1) from i = 1,j = 2 to start scanning backward, if the scan of n string is still equal, indicating that s is only composed of 1 characters, then the output of the number 1 can be;
(2) If the i+k and j+k found unequal, if a[i+k]>a[j+k], so i = i+k+1. If i = j at this time, then I = i+1. If A[i+k]<a[j+k], make j = j+k+1. If i = j at this time, then j = j+1;
(3) After scanning, if I>N,A[J] is the smallest representation, if j>n,a[i] is the smallest representation.
The algorithm has a time complexity of O (n).
1#include <iostream>2#include <string.h>3#include <cstdio>4 Const intMAXS = 1e4+5;5 using namespacestd;6 intN,ans;7 Chars[maxs*2];8 intMain () {9scanf"%d",&n);Ten while(n--){ Onescanf"%s", s+1); A intLENS = strlen (s+1); - for(inti =1; I <= lens; ++i) S[lens+i] =S[i]; - inti =1, j =2, K; the while(I <= Lens && J <=lens) { - for(k =0; k <= Lens && s[i+k]==s[j+k]; k++); - if(k >= Lens) Break; - if(S[i+k] > s[j+K]) { +i = i+k+1; - if(i = = j) i++; +}Else{ Aj = j+k+1; at if(i = = j) J + +; - } - } -Ans =min (i,j); -printf"%d\n", ans); - } in return 0; -}
View Code
"POJ1509" Glass beads