环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)

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

CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等。

在这些表示法中,字典序最小的称 为"最小表示"。 输入一个长度为n(n≤100)的环状DNA串(只包含A、C、G、T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示。

例如,CTCC的最小表示是 CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC。

【分析】

本题出现了一个新概念:字典序。所谓字典序,就是字符串在字典中的顺序。一般地, 对于两个字符串,从第一个字符开始比较,当某一个位置的字符不同时,该位置字符较小的串,字典序较小(例如,abc比bcd小);如果其中一个字符串已经没有更多字符,但另一个字符串还没结束,则较短的字符串的字典序较小(例如,hi比history小)。字典序的概念可 以推广到任意序列,例如,序列1, 2, 4, 7比1, 2, 5小。 学会了字典序的概念之后,本题就不难解决了:就像"求n个元素中的最小值"一样,用变量ans表示目前为止,字典序最小串在输入串中的起始位置,然后不断更新ans。

#include<cstdio>
#include<cstring>
#define maxn 105
using namespace std;
int ans[maxn];//ans数组含义:在y处存了y的最小生成元
bool less(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(){
    char s[maxn];
    scanf("%s",s);
    int ans=0;
    int n=strlen(s);
    for(int i=0;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;
}

注意环状在数据结构上的表示!!

原文地址:https://www.cnblogs.com/LOW-ctfer/p/10386160.html

时间: 2024-08-28 23:57:45

环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)的相关文章

【紫书】例题3-6 环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)

[题目描述] 长度为n的环状串有n种表示法,分别为某个位置开始顺时针得到.例如,图中的环状串有10种表示: CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等.在这些表示法中,字典序最小的称为"最小表示". 输入一个长度为n(n<=100)的环状DNA串(只包含A.C.G.T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示.例如,CTCC的最小表示是CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC. 输入: 在输入文件的第一行 为序列数量.

环状序列(CircularSequence,ACM/ICPC Seoul 2004,UVa1584)

Question 例题3-5 环状序列(CircularSequence,ACM/ICPC Seoul 2004,UVa1584) 长度为n的环状串有n种表示方法,分别为从某个位置开始顺时针得到,在这些排列中字典顺序最小的称"最小表示". 如CTCC的最小表示为CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC. 提示:对于两个字符串,从第一的字符开始比较,当某一个位置的字符不同时,该位置字符较小的串,字典序小,如果一个字符串没有更多的字符,但是另一个字符串还没结束,则较短

最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)

Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n(1<=n<=100000), 求最小生成元.无解输出0.例如,n=216,121,2005时的解分别是198,0,1979. Think 方法一:假设所求生成元记为m,不难发现m<n.换句话说,只需枚举所有的m<n,看看有木有哪个数是n的生成元.此举效率不高,因为每次计算一个n的生成元

分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf("%s", s); double sum = 0; for (int i = 0; i < strlen(s); i++) { if (s[i] == 'C') sum += (s[i + 1] - 48) * 12.01; if (s[i] == 'H') { if (s[i +

生成元(Digit Generator,ACM/ICPC Seoul 2005, UVa1583)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of

得分(Score, ACM/ICPC Seoul 2005,UVa 1585)

#include<cstdio>#include<cstdlib>#include<cstring>int main(){ char s[80];//输入OOXXOXXOOO,最终得分计算为1+2+0+0+1+0+0+1+2+3=10 int m = 0, sum = 0, i = 0; scanf("%s", s); for (i = 0; i < strlen(s); i++) { if (s[i] == 'X') m = 0; if (s

生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)

#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, b, ans, l;int main(){ scanf("%d", &t);//这句话是为了确定一个最大的范围,比如说10000 while (t--) { scanf("%d", &n); ans = 0; for (int i = n - 50;

分子量(Molar Counting, ACM/ICPC Seoul 2007, UVa1586)

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from

得分(Score,ACM/ICPC Seoul 2005,UVa 1585)

#include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getchar(); while(t--) { cou=sum=0; while((b=getchar())!='\n') { if(b=='O')sum+=++cou; else cou=0; } printf("%d\n",sum); } return 0; }