uva 1584 Circular Sequence (字符串处理)

C - Circular Sequence
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

Description

Download as PDF
 

Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence ``CGAGTCAGCT", that is, the last symbol ``T" in ``CGAGTCAGCT" is connected to the first symbol ``C". We always read a circular sequence in the clockwise direction.

 

\epsfbox{p3225.eps}

Since it is not easy to store a circular sequence in a computer as it is, we decided to store it as a linear sequence. However, there can be many linear sequences that are obtained from a circular sequence by cutting any place of the circular sequence. Hence, we also decided to store the linear sequence that is 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 in the figure, the lexicographically smallest sequence is ``AGCTCGAGTC". If there are two or more linear sequences that are lexicographically smallest, you are to find any one of them (in fact, they are 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 that is written as an arbitrary linear sequence. Since the circular sequences are DNA sequences, only four symbols, ACG and T, are allowed. Each sequence has length at least 2 and at most 100.

 

Output 

Print exactly one line for each test case. The line is to contain the lexicographically smallest sequence for the test case.

The following shows sample input and output for two test cases.

 

Sample Input 

 

2                                     
CGAGTCAGCT                            
CTCC

 

Sample Output 

 

AGCTCGAGTC 
CCCT

问一个循环的字符串的最小字典序....
好像没说清== 
这题的关键在于想到用substr...
感觉c++的string 比c 的字符数组高到不知哪里去了...
 1 /*************************************************************************
 2     > File Name: code/uva/1584.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月15日 星期二 17时04分52秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 const int N=1E2+7;
32 string st;
33 string ans;
34 int len;
35 int main()
36 {
37   #ifndef  ONLINE_JUDGE 
38     freopen("in.txt","r",stdin); 
39   #endif
40     int T;
41     cin>>T;
42     while (T--)
43     {
44     cin>>st;
45     len = st.length();
46      ans = st;
47     for (int i = 0 ; i < len ; i++)
48     {
49         string tmp =st.substr(i+1)+st.substr(0,i+1);
50         if (tmp<ans) ans = tmp;
51     }
52     cout<<ans<<endl;
53 
54     
55     }
56   
57   
58  #ifndef ONLINE_JUDGE  
59   fclose(stdin);
60   #endif
61     return 0;
62 }
View Code

 



posted @ 2015-09-15 19:39  111qqz  阅读(286)  评论(0编辑  收藏  举报