码迷,mamicode.com
首页 > 其他好文 > 详细

Circular Sequence UVA - 1584

时间:2021-02-01 12:18:46      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:always   forms   symbols   mos   sts   最小   and   direct   cte   

?   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

HINT

?   题目的大致意思是让你一个循环的字符串,从哪里看过去是最小的,用标准库函数来比较大小。这里采用将字符串复制一倍的办法来求出循环字符串中最小的一段。例如CTCC:技术图片

此时字符串的长度位原来的二倍,将从每一个向后4个长度的字符串用来和已知最小的字符串比较。

Accepted

#include<stdio.h>
#include<string.h>

int main()
{
    int sum;
    scanf("%d", &sum);
    while (sum--)
    {
        char arr[201];
        scanf("%s", arr);
        int len = strlen(arr);
        strncpy(&arr[len], arr,len);		//将字符串扩大复制一倍拼接在末尾
        char min[101];					   //用来保存最小的字符串
        strncpy(min, arr, len);				//对存储最小字符串的数组进行初始化
        for (int i = 0;i < len;i++)			//比较
        {
            if (strncmp(min, &arr[i], len) > 0)
                strncpy(min, &arr[i], len);

        }
        min[len] = ‘\0‘;					//在字符串的末尾补上‘\0‘
        printf("%s\n", min);
    }
}
        min[len] = ‘\0‘;	//在字符串的末尾补上‘\0‘
        printf("%s\n", min);
    }
}

Circular Sequence UVA - 1584

标签:always   forms   symbols   mos   sts   最小   and   direct   cte   

原文地址:https://www.cnblogs.com/3236676588buladuo/p/14350322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!