UVA 1584 环状序列

题目链接:https://vjudge.net/problem/UVA-1584

题目描述:长度为n环状串有n种表示方法,例如CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等,在这些表示方法中,字典序最小的称为最小表示。

输入长度为n的环状串(只含AGCT),要求输出最小表示。

字典序:参看https://zh.wikipedia.org/wiki/%E5%AD%97%E5%85%B8%E5%BA%8F

参考代码:

#include<iostream>
#include <stdio.h>
#include <string.h>
#define Max 110
char Seq[Max];
int Less(const char *s,int p,int q) {
	int len = strlen(s);
	for(int i = 0; i < len; i++) 
		if(s[(p+i)%len]!=s[(q+i)%len])
			return s[(p+i)%len]<s[(q+i)%len];//成立返回1,其他返回0
	return 0;//相等时候的情况
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%s",Seq);
		int temp = 0;//标记当前位置
		int len = strlen(Seq);
		for(int i = 1; i < len; i++)
			if(Less(Seq,i,temp)) temp = i;
		for(int i = 0; i < len; i++)
			putchar(Seq[(temp+i)%len]);//对长度求余是因为temp+i有可能大于len 
		putchar('\n');	
		}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40624026/article/details/81383259