UVa 1584 Circular Sequence(环形串最小字典序)

题意  给你一个环形串   输出它以某一位为起点顺时针得到串的最小字典序

直接模拟   每次后移一位比较字典序即可  注意不能用strcpy(s+1,s)这样后移  strcpy复制地址不能有重叠部分

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 150;
char s[N], ans[N], c;
int t, l;  

int main()
{
    scanf ("%d", &t);
    while (t--)
    {
        scanf ("%s", s);
        l = strlen (s);
        strcpy (ans, s);
        for (int i = 0; i < l; ++i)
        {
            c = s[l - 1];
            for (int j = l - 1; j >= 1 ; --j)
                s[j] = s[j - 1];
            s[0] = c;
            if (strcmp (s, ans) < 0)
                strcpy (ans, s);
        }
        printf ("%s\n", ans);
    }
} 

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,CG 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



时间: 2024-10-13 03:03:28

UVa 1584 Circular Sequence(环形串最小字典序)的相关文章

UVA 1584 - Circular Sequence(环状序列)(字典序)

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 is, the last symbol ``T" in ``CGAGTCAGCT" is connected to the firs

UVa 1584 Circular Sequence --- 水题

UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较二者字典序大小的函数, 然后再用一层循环,进行n次比较,保存最小的字典序的串的首字母位置,再利用模运算输出即可 /* UVa 1584 Circular Sequence --- 水题 */ #include <cstdio> #include <cstring> //字符串s为环状,

uva 1584 Circular Sequence (字符串处理)

C - Circular Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence ``CGAGTCAGCT", that is,

UVa -1584 Circular Sequence 解题报告

1.题目大意 输入长度为n$(2\le n\le 100)$的环状DNA串,找出该DNA串字典序最小的最小表示. 2.思路 这题特别简单,一一对比不同位置开始的字符串的字典序,更新result. 3.代码 #include"stdio.h" #include"string.h" #define maxn 100 int judge(char* s,int p,int q) //比较p的字典序是否比q小 { int m=strlen(s); for(int i=0;

UVa 1584 - Circular Sequence

哈哈哈哈,  又用Java强大的字符串API和集合类水过一道题,不服用C++来一遍看看比这个代码长多少(不考虑时间的情况下). import java.util.*; public class Main1584 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<String> arrlist = new ArrayList<String>(); St

UVa 1548 Circular Sequence (字符串处理)

Circular Sequence Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence ``CGAGTCAGCT", that is, t

UVA 1584 环状序列

题意: 给定一个环状字符串,输出字典序最小的线装字符串. 分析: 我一开始是将原字符串*2去模拟环,然后分别截取以字符串不同字母为首的子串,然后用sort去排序输出最小的串,复杂度为O(n^2 + nlogn)吧. 然后看了紫书的题解,用了一个函数去枚举比较每一个字母为开头的子串和预估答案的子串的字符串字典序大小,枚举串的某一个字母的使整个串字符串小于另一个串(他们长度都是一样的,只要其中一个小,那么整个就会小,因为字典序是取决前面的字母的)就立刻更新ans,然后他用的是下标mod长度,最坏复杂

UVA 116 Unidirectional TSP(DP最短路字典序)

Description  Unidirectional TSP  Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Travel

拼接最小字典序

转载请标明出处http://www.cnblogs.com/haozhengfei/p/63b2460bceae42e55f0c2d150bde663b.html 拼接最小字典序 拼接最小字典序练习 第8节 拼接最小字典序练习题 对于一个给定的字符串数组,请找到一种拼接顺序,使所有小字符串拼接成的大字符串是所有可能的拼接中字典序最小的. 给定一个字符串数组strs,同时给定它的大小,请返回拼接成的串. 测试样例: ["abc","de"],2 "abcde