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.
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, A, C, G 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
题目类型:字符处理题
算法分析:枚举可能出现的所有情况,用变量flag将目前为止的字典序最小的串记住,然后不断更新flag,直到最后将flag所指的字符串输出即可,下面给出两种实现,第一种通过倍增数组实现(推荐),第二种通过循环节实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
/************************************************** filename :f.cpp author :maksyuki created time :2017/12/1 12:48:04 last modified :2017/12/1 12:59:53 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; char s[maxn], v[maxn], tmp[maxn], ans[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int t; scanf("%d", &t); while(t--) { scanf("%s", s); strcpy(v, s); strcat(v, s); int len = strlen(s); memset(ans, 0, sizeof(ans)); for(int i = 0; i < len; i++) { memcpy(tmp, v + i, len); tmp[len] = 0; //important if(!strlen(ans) || strcmp(ans, tmp) > 0) strcpy(ans, tmp); } puts(ans); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <cstdio> #include <iostream> #include <fstream> #include <cstring> using namespace std; #define maxn 106 char ans[maxn]; int least (const char *ans, int p, int q) { int len = strlen (ans); int i; for (i = 0; i < len; i++) { if (ans[(p+i)%len] != ans[(q+i)%len]) return ans[(p+i)%len] < ans[(q+i)%len]; } return 0; } int main() { int cases; //ifstream cin ("aaa.txt"); cin >> cases; while (cases--) { cin >> ans; int i, flag = 0; int len = strlen (ans); for (i = 1; i < len; i++) if (least (ans, i, flag)) flag = i; for (i = 0; i < len; i++) printf ("%c", ans[(flag+i)%len]); printf ("\n"); } return 0; } |
- « 上一篇:uva1368
- uva1587:下一篇 »