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

题目大意:给你一个环状字符串,你可以从任意起点开始读,要求找出字典序最小的字符串。

思路:从字符串的每一个字符str[i]开始读字符串,让它和上一个字典序最小的比较,如果比上一个的字典序还小,那么就将minn更新为这个字符串的str[i]的i,然后继续比较。

这个函数的实现比较相似,毕竟是相同的思路

函数:

bool findmin(int n,int m)//查看以谁为新的字符串起点字典序最小
{
    int len=strlen(str);
    for(int i=0;i<len;i++)
        if(str[(n+i)%len]!=str[(m+i)%len])
            return str[(n+i)%len]<str[(m+i)%len];
    return false;//相等的情况
}

完整程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[105];
bool findmin(int n,int m)//查看以谁为新的字符串起点字典序最小
{
    int len=strlen(str);
    for(int i=0;i<len;i++)
        if(str[(n+i)%len]!=str[(m+i)%len])
            return str[(n+i)%len]<str[(m+i)%len];
    return false;//相等的情况
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>str;
        int minn=0,len=strlen(str);
        for(int i=0;i<len;i++)//访问字符串的每一位
            if(findmin(i,minn)) minn=i;//更新为大字典序的位置
            for(int i=0;i<len;i++)
                printf("%c",str[(i+minn)%len]);
            printf("\n");
    }
    return 0;
}
时间: 2024-08-03 19:19:55

UVA 1584 - Circular Sequence(环状序列)(字典序)的相关文章

UVa 1584 Circular Sequence --- 水题

UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较二者字典序大小的函数, 然后再用一层循环,进行n次比较,保存最小的字典序的串的首字母位置,再利用模运算输出即可 /* UVa 1584 Circular Sequence --- 水题 */ #include <cstdio> #include <cstring> //字符串s为环状,

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 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 解题报告

1.题目大意 输入长度为n$(2\le n\le 100)$的环状DNA串,找出该DNA串字典序最小的最小表示. 2.思路 这题特别简单,一一对比不同位置开始的字符串的字典序,更新result. 3.代码 #include"stdio.h" #include"string.h" #define maxn 100 int judge(char* s,int p,int q) //比较p的字典序是否比q小 { int m=strlen(s); for(int i=0;

UVa 1584 - Circular Sequence

哈哈哈哈,  又用Java强大的字符串API和集合类水过一道题,不服用C++来一遍看看比这个代码长多少(不考虑时间的情况下). import java.util.*; public class Main1584 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<String> arrlist = new ArrayList<String>(); St

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

UVA 1584 环状序列

题意: 给定一个环状字符串,输出字典序最小的线装字符串. 分析: 我一开始是将原字符串*2去模拟环,然后分别截取以字符串不同字母为首的子串,然后用sort去排序输出最小的串,复杂度为O(n^2 + nlogn)吧. 然后看了紫书的题解,用了一个函数去枚举比较每一个字母为开头的子串和预估答案的子串的字符串字典序大小,枚举串的某一个字母的使整个串字符串小于另一个串(他们长度都是一样的,只要其中一个小,那么整个就会小,因为字典序是取决前面的字母的)就立刻更新ans,然后他用的是下标mod长度,最坏复杂

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

uva 1626 Brackets Sequence ?(动态规划)

状态表示方法:d[ i ][ j ]表示的是一条序列的开始和结束: 状态定义:d[ i ][ j ]表示字串s[ i~j ] 需要添加的数量. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; char s[105]; int d[105][105]; bool match(char ch1,char ch2) { if((ch1=='['&&am