UVa 1584 - Circular Sequence

来源:互联网 发布:e3 1230 v2淘宝 编辑:程序博客网 时间:2024/04/29 08:52

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 casesT 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≤100)的环状DNA串(只包含A、C、G、T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示。例如,CTCC的最小表示

CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC。

#include<stdio.h>#include<string.h>#define maxn 105int less(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])     return s[(p+i)%n] < s[(q+i)%n];  }  return 0; //表示相等}int main(){  int t;  char s[maxn];  scanf("%d", &t);  while(t--) {  scanf("%s", s);  int ans = 0;  int n = strlen(s);  for(int i = 1; i < n; i++)     if(less(s, i, ans))  ans = i;  for(int i = 0; i < n; i++)     putchar(s[(i+ans)%n]);   putchar('\n');  }return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新鞋前面太硬怎么办 新鞋子鞋底太硬怎么办 不到一米八的身高想扣篮怎么办 鞋胶粘在鞋面上怎么办 鞋子上沾了胶怎么办 休闲鞋号码大了半码怎么办 高跟鞋大了一码怎么办 浅口单鞋买大了怎么办 新鞋子磨大脚趾怎么办 鞋前面磨大脚趾怎么办 白鞋子蹭黑了怎么办 夏天穿皮鞋捂脚怎么办 耐克赤足掉漆怎么办 鞋子买回来小了怎么办 布鞋大了一码怎么办 鞋子买小了一码怎么办 运动鞋小了一码怎么办 帆布鞋小了一码怎么办 脚踝骨韧带断了怎么办 咖啡喝多了失眠怎么办 奥迪q7电瓶没电怎么办 一岁宝宝坐不稳怎么办 2岁宝宝不肯把尿怎么办 踢足球上肢和下肢不协调怎么办 ppt文字放映时重叠怎么办 月子8天腰背疼怎么办 生完五天腰背疼怎么办 生完孩子腰不好怎么办 养了个白眼狼怎么办 孩子学东西很慢怎么办 手不小心碰肿了怎么办 腿中间摩擦的疼怎么办 晚上睡觉时双腿酸胀怎么办 腿擦破皮了怎么办 快速好 宝宝腿擦破皮了怎么办 新生儿睡觉腿喜欢弯曲怎么办 孩子八个月交叉走路怎么办 胫椎引起双腿发热怎么办 婴儿头型睡扁了怎么办 膝盖抻筋了疼怎么办 小腿肚子抻筋了怎么办