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

长度为n的环状串有n种表示法,分别为从某个位置开始顺时针得到。例如,图3-4的环状串有10种表示:

CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等。在这些表示法中,字典序最小的称为”最小表示”。

输入一个长度为n(n≤100)的环状DNA串(只包含A、C、G、T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示。例如,CTCC的最小表示是CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC。

#include <stdio.h>
#include <string.h>
#define maxNum 105// 判断以位置p开头的小还是以位置q开头有的小
int comp(const 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]) {// 位置q小于位置p返回true// 大于返回falsereturn s[(p + i) % n] < s[(q + i) % n];}}// 相等情况return 0;
}int main() {int T,N;scanf("%d", &T);while(T--) {char s[maxNum];scanf("%s", s);int n = strlen(s);// 记录最小字典序开始位置int pos = 0;for(int i = 0; i < n; i++) {if(comp(s, i, pos)) {pos = i;}}for(int i = 0; i < n; i++) {putchar(s[(pos + i) % n]);}putchar('\n');}return 0;
}

1584 - Circular Sequence相关推荐

  1. 【UVA/Codeforces】1584 Circular Sequence / 792B Counting-out Rhyme(就是一个圈儿...)

    https://vjudge.net/problem/UVA-1584 1584 Circular Sequence 输入一个字符串,可以以字符串中任意一个字母作为起始,输出字典序最小的那个字符串 两 ...

  2. Circular Sequence UVA - 1584

    原题及翻译 Some DNA sequences exist in circular forms as in the following figure, which shows a circular ...

  3. UVA1584 ​​​​​​​Circular Sequence【字符串】

    Circular Sequence UVA - 1584 题目传送门 题目大意:输入一个环形字符串,需输出其最小字典序的形式的字符串. AC代码: #include <cstdio> #i ...

  4. UVA1584 UVALive3225 Circular Sequence【水题】

      Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequ ...

  5. UVa1584 - Circular Sequence

    //UVa1584 - Circular Sequence //题目:给你一个环状串,输出它以某一位置为起点时得到最小字典序的串. //分析:从初位置到末位置每次更新字典序较小的起始位置 #inclu ...

  6. 例题3-6 环状序列(Circular Sequence)

    我的代码(vjudge AC): //Circular Sequence #include <stdio.h> #include <string.h> bool less(co ...

  7. (艾迪茉莉转圈圈~~找最小环)Circular Sequence UVA - 1584

    #include<iostream> #include<string.h> #include<stdio.h> using namespace std; char ...

  8. 例题3-6 环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)

    长度为n的环状串有n种表示法,分别为从某 个位置开始顺时针得到.例如,图3-4的环状串 有10种表示: CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等.在这些表示法中,字典序最小的 ...

  9. 算法竞赛入门经典(第二版) | 程序3-10 生成元 (UVa1584,Circular Sequence)

    题目概述: 长度为n的环状串有n种表示法, 字典序最小的称为最小表示.输入一个长度为n(n<100)的环状字符串的一种表示方法,输出最小表示 . 如:CTCC 为环状字符串的一种表示方法,它的所 ...

最新文章

  1. YOLOV4各个创新功能模块技术分析(三)
  2. code blocks 快捷键
  3. linux shell脚本写法,linux: 常用shell脚本写法
  4. NotificationManagerService使用详解与原理分析(一)
  5. liunxu mysql_Liunx下安装MySql
  6. OJ1030: 判断直角三角形
  7. Nginx学习之六-nginx核心进程模型
  8. ACL 2019 | 清华与华为提出ERNIE:知识图谱结合BERT才是「有文化」的语言模型
  9. yourphp超出20记录自动删除
  10. 北京地区广播电台频率
  11. Speedoffice(Excel)行高列宽自动调整的方法
  12. 苦熬31年终于登陆科创板!WPS如何一步步熬到了今天?
  13. 《数据库系统工程师》备考指南
  14. centos 开发套件_替代的Laravel套件开发工作流程
  15. Qt主线程和工作线程更新界面问题
  16. Hyperview二次开发:模态阵型的自动排列、输出GIF、输出PPT等
  17. 优化方法之黄金分割法(Python实现)
  18. python通过ssh通道连接PostgreSQL数据库(mysql等类同)
  19. Python爬虫实现无api版本各国汇率实时兑换(美元、英镑等)
  20. Redis设计与实现学习记录《一》

热门文章

  1. 【C语言】实参求值的顺序
  2. C#LeetCode刷题之#219-存在重复元素 II​​​​​​​(Contains Duplicate II)
  3. webview中js调用客户端
  4. lighthouse使用_如何在CircleCI中使用Lighthouse
  5. 游戏窗口组合键消息失败_5失败的投资组合,以后我在这里
  6. ggplot2柱状图
  7. Java 统计字母个数
  8. 主机屋linux怎么连,全网最详细的samba文件共享服务!
  9. asp建站系统源码_ASP.NET制造业进销存管理系统源码
  10. 10个Python面试常问的问题