UVA1584-Circular Sequence(紫书例题3.6)
Some DNA sequences exist in circular forms as in
the following gure, which shows a circular sequence
\CGAGTCAGCT", that is, the last symbol \
T
" in
\
CGAGTCAGCT
" is connected to the rst symbol \
C
". We al-
ways 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 nd the lexicographically smallest sequence
from a given circular sequence. For the example in the gure,
the lexicographically smallest sequence is \
AGCTCGAGTC
". If there are two or more linear sequences that
are lexicographically smallest, you are to nd 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 rst line of the input
le. 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. 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.
Sample Input
2
CGAGTCAGCT
CTCC
Sample Output
AGCTCGAGTC
CCCT
//思路:利用strcmp函数比较字典序,如果找到小的就复制过去,然后将节点向后移一位,继续比较;
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char s[110],s1[110];
int t;
cin>>t;
while(t--)
{
cin>>s;
int l=strlen(s);
strcpy(s1,s); //先将s复制到s1数组
for(int i=0;i<l;i++)
{
char lastc=s[l-1]; //最后一个字符,待会儿赋值给s[0]
for(int j=l-1;j>0;j--)
s[j]=s[j-1]; //相当于往后断开一个节点
s[0]=lastc;
if(strcmp(s,s1)<0) //如果新串字典序较小,复制给s1
strcpy(s1,s);
}
cout<<s1<<endl;
}
return 0;
}
标程:
#include<stdio.h>
#include<string.h>
#define maxn 105
// 环状串s的表示法p是否比表示法q的字典序小
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;
}
UVA1584-Circular Sequence(紫书例题3.6)的更多相关文章
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
- 紫书 例题8-3 UVa 1152(中途相遇法)
这道题要逆向思维, 就是求出答案的一部分, 然后反过去去寻找答案存不存在. 其实很多其他题都用了这道题目的方法, 自己以前都没有发现, 这道题专门考这个方法.这个方法可以没有一直往下求, 可以省去很多 ...
- 紫书 例题8-12 UVa 12627 (找规律 + 递归)
紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...
- 紫书 例题8-4 UVa 11134(问题分解 + 贪心)
这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...
- 紫书 例题8-17 UVa 1609 (构造法)(详细注释)
这道题用构造法, 就是自己依据题目想出一种可以得到解的方法, 没有什么规律可言, 只能根据题目本身来思考. 这道题的构造法比较复杂, 不知道刘汝佳是怎么想出来的, 我想的话肯定想不到. 具体思路紫书上 ...
- 紫书 例题 9-5 UVa 12563 ( 01背包变形)
总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...
- uva1584 Circular Sequence(Uva-1584)
vj:https://vjudge.net/problem/UVA-1584 这个题讲的是一个圆环,圆环上面有一堆字母,找出字典序最小的那一圈 这个题我觉得直接用c语言的strcmp那一套感觉真是用不 ...
- UVA489 - Hangman Judge【紫书例题4.2】
题意:就是给出一个字符串,让你去一个一个猜测,相同字母算一次,如果是之前猜过的也算错,如果你在错7次前猜对就算你赢,文章中是LRJ的例题代码. #include<stdio.h> #inc ...
- UVA272-TEX Quotes(紫书例题3.1)
TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few ty ...
随机推荐
- WEBGL学习【九】立方体贴不同的纹理
<html> <!--开始实现一个三维街景的渲染效果--> <head> <meta http-equiv="Content-Type" ...
- C语言求大数的阶乘
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,该如何计算? 当一个数很大时,利用平常的方法是求不出来它的阶乘的,因为数据超出了范围.因此我们要用数组来求一个大数的阶乘,用数组的每位表示结果的 ...
- [USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)
题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...
- [luogu P2756 ] 飞行员配对方案问题 (最大流)
强行做裸题做了两个小时..我果然太水了QAQ 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 ...
- JavaScript CSS 实现简单的 TAB 标签切换
使用CSS隐藏所有tab页,然后使用JavaScript给选中的元素对应ID的tab页设置class="active"类来显示该元素,以此实现tab切换. 如鼠标放置到shwww时 ...
- flash透明 处于最低
怎样在html中让flash透明 前提是FLASH里没有用其它形状或图形来作为背景.方法主要是在网页中的Flash加入一个参数,让网页设定Flash文件背景透明,Flash文件本身做不到. 关键: & ...
- HelloWorld编译正常运行报noclassdeffounderror
修改环境变量classpath: 原理: classpath是搜索java类库的路径:当你输入命令“java HelloWorld“时,会根据classpath寻找HelloWorld.class:一 ...
- iterator遍历list理解
1.iterator的next()方法返回值,就是你指定的iiterator<>泛型.所以你再强制转换,就可以得到list里的item了,直接是item对象了. 2.list这东西,你ne ...
- HDU 5171
这道题本来很水,以前做过一样的,斐波那契数列,用矩阵快速幂的方法求,本来很水,以前做过很多次,为毛做的时候没想到T_T #include <iostream> #include <c ...
- hdu 5410 CRB and His Birthday 01背包和全然背包
#include<stdio.h> #include<string.h> #include<vector> #include<queue> #inclu ...