zoukankan      html  css  js  c++  java
  • Circular Sequence UVA 1584

    原题及翻译

    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”.
    如下图所示,一些DNA序列以圆形存在,显示了一个圆形序列“cgagtacgt”,即“cgagtacgt”中的最后一个符号“t”与第一个符号“c”相连。
    在这里插入图片描述
    We always read a circular sequence in the clockwise direction.
    我们通常在顺时针方向读取一个圆形序列。
    Since it is not easy to store a circular sequence in a com- puter as it is, we decided to store it as a linear sequence.
    由于在计算机中存储循环序列并不容易,因此我们决定将其存储为线性序列。
    However, there can be many linear sequences that are ob- tained 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”.
    词典最小的序列是“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.
    输入由T测试用例组成。
    The number of test cases T is given on the first line of the input file.
    一行给出测试用例数t。
    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.
    由于圆形序列是DNA序列,因此只允许使用“A”、“C”、“G”和“T”四个符号。
    Each sequence has length at least 2 and at most 100.
    每个序列的长度至少为2,最多为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

    题目理解

    长度为n的环状序列有n种表示方法,分别为从某个位置开始顺时针得到。其中,字典序最小的称为“最小表示”。
    输入一个长度为n(n<=100)的环状DNA串(只包含A,C,G,T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示。

    思路

    所谓的字典序,就是字符串在字典中的顺序。

    一般地,对于两个字符串,从第一个字符开始比较,当某一个位置的字符不同时,该位置字符较小的串,字典序较小;如果其中一个字符串已经没有更多字符,但另一个字符串还没结束,则较短的字符串的字典序较小。

    字典序的可以推广到任意序列。

    代码

    #include <stdio.h>
    #include <string.h>
    #define maxn 105
    int 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;
    }
    
  • 相关阅读:
    主机无法ping通网关,但可以ping通局域网内的其他主机
    linux下安装mysql
    国内python源
    在vue中引入element-ui时报错
    在windows上部署vue
    linux上部署vue开发环境
    vue插件之vue-router路由基本使用
    vue监听属性变化
    vue过滤器
    vue组件的使用
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12339480.html
Copyright © 2011-2022 走看看