UVa 1584 Circular Sequence 【数组和字符串】
2017-01-25 19:40
239 查看
Time limit: 3.000 seconds
Description
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.
Sample Input
2
CGAGTCAGCT
CTCC
Sample Output
AGCTCGAGTC
CCCT
题意:所输入字符串为环状序列,输出每个字符串的最小字典序表示(详情可参考刘汝佳编著的《算法竞赛入门经典(第二版)》P52)
AC代码:
Description
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.
Sample Input
2
CGAGTCAGCT
CTCC
Sample Output
AGCTCGAGTC
CCCT
题意:所输入字符串为环状序列,输出每个字符串的最小字典序表示(详情可参考刘汝佳编著的《算法竞赛入门经典(第二版)》P52)
AC代码:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; int les(char* s, int p, int q) { int n = strlen(s); for(int i = 0; i < n; ++i) { if(s[(p+i)%n] != s[(q+i)%n]) return s[(p+i)%n] < s[(q+i)%n]; } return 0; } int main() { char s[105]; int n, ans, len; while(~scanf("%d",&n)) { while(n--) { ans = 0; scanf("%s",s); len = strlen(s); for(int i = 0; i < len; ++i) if(les(s, i, ans)) ans = i; for(int i = 0; i < len; ++i) cout << s[(ans+i)%len]; cout << endl; } } return 0; }
相关文章推荐
- uva445string与字符串数组和单个字符逐个输入
- 求出长度为1, 2, 3, 4, 5....的字符串最大出现次数 后缀数组 UVA 11855 - Buzzwords
- 数组字符串uva 10405 - Longest Common Subsequence(最长公共子序列)
- Uva 227 - Puzzle (数组和字符串)
- UVA 1584 字符串
- uva1586 字符串与数组03-习题2
- 连续子串中出现超过一半次数的字符串 后缀数组 uva 11107 Life Forms
- 字符串与数组03-例题3 Palindromes,UVa401
- 字符串与数组03-例题-2 (WERTYU,UVa 10082)
- uva10132 字符串数组的qsort排序技巧
- UVa 227 Puzzle 【数组和字符串】【模拟】
- UVa1583,UVa1584(环形数组)
- 字符串转成字符数组,同时控制输出字符数——ToCharArray、Response.Write新用法(2006-04-20 15:32)
- VB中字符串数组快速复制的一种方法
- 数组下标是字符串的实现
- 用字符串数组实现的大数运算
- 传递自动化字符串数组
- 字符串 To 字节数组 的转换
- Java2实用教程(第二版)程序代码——第五章 数组与字符串
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)