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

CODE

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

int main()
{
  char s[105],s1[105];
  int N;
  scanf("%d",&N);
  while(N!=0)
  {
    scanf("%s",s);
    int len=strlen(s);
    strcpy(s1,s);
    char s0[105];
    strcat(s,s1);
    strcpy(s0,s);
    char ch[105];
    int i,j;
    for(i=0; i<len; i++)
    {
      int k=0;
      for(j=i; j<len+i; j++)
      {
        ch[k]=s0[j];
        k++;
      }
      int pd=strcmp(s1,ch);
      if(pd>0)
      strcpy(s1,ch);
      memset(ch,0,len+5);
    }
    printf("%s\n",s1);
    N--;
  }
  return 0;
}

MY WAY其实对我来说,这个题耽搁了好久。因为一些其他原因没有及时做完。每当想起这个题来总会有一些乱七八糟的事情发生,让我难以好好做题,从微博的时间上就可以看出来的。这个题的思路还是比较简单的:首先把输入的字符串复制一遍再接上,然后通过循环来依次找出最合适的字符串再输出。这个题很好的运用了字符串函数,比如strlen,strcmp,strcpy,strcat……这些统统用到了,这真是一个有知识含量的题啊。呵呵。
时间: 2025-01-05 22:25:52

Problem 006——Circular Sequence的相关文章

poj Problem A. Score Sequence(暴力)

这道题,对于我这种英文不好的人来说,有点费劲啊. 题目的意思:给你两组成绩,你要找出他们之间最大的公共子序列,不能有重复,然后输出两组数据. 第一组就是:按照从大到小输出最大子序列. 第二组就是:按照个位数由小到大输出,若个位数一样大,则小的在前输出最大子序列. 解题思路基本上已经出来了,就是千万要注意就是在最长子序列匹配之前就就把重复的数字给删除. 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h

UVA1584 UVALive3225 Circular Sequence

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

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

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

Problem Longest Consecutive Sequence

Problem Description: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your alg

Problem A Number Sequence(KMP基础)

A - Number Sequence Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1711 Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 100