​   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的更多相关文章

  1. UVa 1584 Circular Sequence --- 水题

    UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...

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

    题意  给你一个环形串   输出它以某一位为起点顺时针得到串的最小字典序 直接模拟   每次后移一位比較字典序就可以  注意不能用strcpy(s+1,s)这样后移  strcpy复制地址不能有重叠部 ...

  3. UVA.1584 环状序列

    UVA.1584 环状序列 点我看题面 题意分析 给出你一段换装DNA序列,然后让你输出这段环状序列的字典序最小的序列情况. 字典序字面意思上理解就是按照字典编排的序列,其实也可以理解为按照ASCII ...

  4. uva 1584.Circular Sequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. Circular Sequence,ACM/ICPC Seoul 2004,UVa 1584

    #include <stdio.h> #include <string.h> #define maxn 105 int lss(const char *s,int p,int ...

  6. 字典序UVa 1584 Circular Sequence

    #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> ...

  7. UVa -1584 Circular Sequence 解题报告 - C语言

    1.题目大意 输入长度为n$(2\le n\le 100)$的环状DNA串,找出该DNA串字典序最小的最小表示. 2.思路 这题特别简单,一一对比不同位置开始的字符串的字典序,更新result. 3. ...

  8. 【例题3-6 UVA - 1584】Circular Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不用真的把每一位都取出来. 用一个后缀的思想. 把原串复制一遍接在后面,然后把每个字符串 都当成一个长度为n的后缀就好了. 比较每个 ...

  9. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...

随机推荐

  1. eclipse从接口快速跳转到实现类

    1.只跳转到实现类上 按住Ctrl键,把鼠标的光标放在要跳转的接口上面,选择第二个 2.直接跳转大实现的方法上 按住Ctrl键,把鼠标的光标放在要跳转的方法上面,选择第二个 对比可以发现,操作都是一样 ...

  2. AWS Switching to an IAM role (AWS CLI)

    一,引言 今天额外分享一篇 AWS 的技术内容,需要在 EC2 切换到跨账号 IAM 角色(AWS CLI).假设我们使用两个 AWS 账户,A账号,B账号.我们希望允许 A 账号用于 "i ...

  3. mac 下如何轻松安装神器 Anaconda

    本文推荐使用homebrew 安装 1.打开终端执行 brew cask install anaconda3 然后就可以喝一杯咖啡了,终端会自动执行安装好 如果终端卡在update homebrew ...

  4. Spring Boot 2.x基础教程:使用MongoDB

    前段时间因为团队调整,大部分时间放在了团队上,这系列的更新又耽误了一下.但既然承诺持久更新,那就不会落下,今天开始继续更新这部分的内容! 过了年,重申一下这个系列的目标:目前主要任务就是把Spring ...

  5. 剑指 Offer 64. 求1+2+…+n + 递归

    剑指 Offer 64. 求1+2+-+n Offer_64 题目描述 题解分析 使用&&逻辑短路规则来终止循环 package com.walegarrett.offer; /** ...

  6. CentOS7 下 MySQL 5.7.23 & XtraBackup 24 做数据备份(1)——安装软件

    在两台机子上同时操作下面的步骤 首先安装MySQL,从官网下载相对应版本的RPM包 mysql-community-client-5.7.23-1.el7.x86_64.rpm mysql-commu ...

  7. day1_安装及建立数据库和表

    #第一份数据库及表create database library; use library; create table book( id int primary key, book_name char ...

  8. VSCode添加用户代码片段,自定义用户代码片段

    在使用VScode开发中经常会有一些重复使用的代码块,复制粘贴也很麻烦,这时可以在VScode中添加用户代码片段,输入简写即可快捷输入. VScode中添加用户自定义代码片段很简单. 1.在VScod ...

  9. 进阶Java多线程

    一.多线程创建方式 1.1.继承Thread类创建线程类 1.实现步骤 定义一个继承Thread类的子类,并重写该类的run()方法: 创建Thread子类的实例,即创建了线程对象: 调用该线程对象的 ...

  10. Java 重入锁和读写锁

    本文部分摘自<Java 并发编程的艺术> 重入锁 重入锁 ReentrantLock,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁.除此之外,该锁还支持获取锁时 ...