UVA1584 UVALive3225 Circular Sequence

Regionals 2004 >> Asia - Seoul

问题链接:UVA1584 UVALive3225 Circular Sequence。基础训练级的题,用C语言编写。

这个问题是寻找循环串中的最小者。

不移动字符串是关键,不然就会浪费时间。

程序中,封装了两个功能函数cirstrcmp()和cirstrprintf(),使得主程序的逻辑大为简化。这两个函数是通用性的函数,完全封装,与全局变量没有关系。

AC通过的C语言程序如下:

/* UVA1584 UVALive3225 Circular Sequence */

#include <stdio.h>
#include <string.h>

#define MAXN 100

/* 循环串比较,a[s]和a[t]开始的两个串进行比较,s>t,s=t,s<t返回值分别为负,0,正 */
int cirstrcmp(char a[], int s, int t, int length)
{
    int count, i, j;

    count = length;
    for(i = s, j = t; count-- > 0; i = ++s % length, j = ++t % length) {
        if(a[i] == a[j])
            continue;
        return a[i] - a[j];
    }

    return 0;
}

void cirstrprintf(char a[], int start, int length)
{
    int count=0, i;

    for(i = start; count++ < length; i = (i + 1) % length)
        putchar(a[i]);
    putchar('\n');
}

int main(void)
{
    int t, min, len, i;
    char s[MAXN+1];

    scanf("%d", &t);
    while(t--) {
        scanf("%s", s);

        len = strlen(s);

        min = 0;
        for(i=1; i<len; i++)
            if(cirstrcmp(s, i, min, len) < 0)
                min = i;

        cirstrprintf(s, min, len);
    }

    return 0;
}
时间: 2024-11-05 20:40:01

UVA1584 UVALive3225 Circular Sequence的相关文章

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 firs

UVa 1584 Circular Sequence --- 水题

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

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(环形串最小字典序)

题意  给你一个环形串   输出它以某一位为起点顺时针得到串的最小字典序 直接模拟   每次后移一位比较字典序即可  注意不能用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 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

Problem 006——Circular Sequence

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 seque

UVa1584 Circular Sequence

#include <stdio.h>#include <string.h> int less(char* str, size_t len, size_t p, size_t q){    size_t i, a, b;    for (i = 0; i < len; ++i)    {        a = (p+i) % len;        b = (q+i) % len;        if (str[a] < str[b])            return

【紫书】例题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. 输入: 在输入文件的第一行 为序列数量.

Circular Sequence UVa1584

描述: 长度为n的环状串有n种表示方法,分别为从某个位置开始顺时针得到,在这些排列中字典顺序最小的称"最小表示". 如CTCC的最小表示为CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC. 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 110 4 5 6 char a[maxn]; //the array entered by users. 7 char b[maxn]; /