1584-circular Sequence
Time limit:3.000 seconds
Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence ' cgagtcagct ', that Was, the last symbol ' T ' in ' CGAGTCAGCT ' was connected to the first symbol ' C '. We always read a circular sequence in the clockwise direction.
Since It is isn't easy to store a circular sequence in a computer as it's, we decided to store it as a linear sequence. However, there can be many linear sequences that is obtained from a circular sequence by cutting any place of the Circula R sequence. Hence, we also decided to store the linear sequence that's lexicographically smallest among all linear sequences that can be obtained from a circular sequence.
Your task is to find the lexicographically smallest sequence from a given circular sequence. For the example of the figure, the lexicographically smallest sequence is ' AGCTCGAGTC '. If there is, or more linear sequences that is lexicographically smallest, you is to find any one of the them (in fact, t Hey is the same).
Input
The input consists of T test Cases. The number of test cases T is given on the first line of the input file. Each test case takes one line containing a circular sequence so is written as an arbitrary linear sequence. Since the circular sequences is DNA sequences, only four symbols, A, C, G and T, are allowed. Each sequence have length at least 2 and at the most 100.
Output
Print exactly one line for each test case. The line was to contain the lexicographically smallest sequence for the test case.
The following shows sample input and output for both test cases.
Sample Input
2 cgagtcagct CTCC
Sample Output
AGCTCGAGTC CCCT
Main topic:give you a ring string, you can start reading from any starting point, asking for the smallest dictionary order string.
Idea: start reading the string from each character of the string str[i], and compare it to the smaller of the previous dictionary, if it is smaller than the previous dictionary order, then update the Minn to the I of the string str[i] and then continue the comparison.
The reality of this function is quite similar, after all, the same idea
Function:
BOOL Findmin (int n,int m)//view the minimum { int len=strlen (str) As the beginning dictionary order for the new string; for (int i=0;i<len;i++) if (str[(n+i)%len]!=str[(m+i)%len]) return str[(n+i)%len]<str[(m+i)%len]; return false;//Equal condition}
Complete Program:
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;char str[105];bool findmin (int n,int m)//view the minimum { int len=strlen (str) as the starting dictionary for the new string; for (int i=0;i<len;i++) if (str[(n+i)%len]!=str[(m+i)%len]) return str[(n+i)%len]<str[(m+i)%len]; Return false;//equal case}int main () { int t; cin>>t; while (t--) { cin>>str; int Minn=0,len=strlen (str); for (int i=0;i<len;i++)//access string for each bit if (findmin (I,minn)) minn=i;//Update to large dictionary order location for (int i=0;i<len;i++) printf ("%c", str[(I+minn)%len]); printf ("\ n"); } return 0;}
UVA 1584-circular Sequence (Ring sequence) (dictionary order)