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, 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
#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 105
using namespace std;

int less1(const char *s ,int p,int q){
    int n = strlen(s);
    for(int i = 0; i < n;i++){
        if(s[(q+i)%n] != s[(p+i)%n])
            return s[(p+i)%n] < s[(q+i)%n];
    }
    return 0;
}

int main(){
    int T;
    while(cin>>T){
        while(T--){
            char s[maxn];
            cin>>s;
            int ans = 0;
            int len = strlen(s);
            for(int i = 1; i < len ; i++){
                if(less1(s,i,ans)) ans = i;
            }
            for(int i = 0; i < len; i++){
                cout<<s[(ans+i)%len];
            }
            cout<<endl;
        }
    }
}
时间: 2024-10-20 04:13:36

UVa 1548 Circular Sequence (字符串处理)的相关文章

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 --- 水题

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

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(环形串最小字典序)

题意  给你一个环形串   输出它以某一位为起点顺时针得到串的最小字典序 直接模拟   每次后移一位比较字典序即可  注意不能用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",

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 -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;

UVA1584 UVALive3225 Circular Sequence

Regionals 2004 >> Asia - Seoul 问题链接:UVA1584 UVALive3225 Circular Sequence.基础训练级的题,用C语言编写. 这个问题是寻找循环串中的最小者. 不移动字符串是关键,不然就会浪费时间. 程序中,封装了两个功能函数cirstrcmp()和cirstrprintf(),使得主程序的逻辑大为简化.这两个函数是通用性的函数,完全封装,与全局变量没有关系. AC通过的C语言程序如下: /* UVA1584 UVALive3225 Cir

UVa 489 Hangman Judge(字符串)

 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo

Problem 006——Circular Sequence

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 seque