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 ...
随机推荐
- C++ STL-stack使用详解
stack 类是容器适配器,它给予程序员栈的功能--特别是 FILO (先进后出)数据结构. 该类模板表现为底层容器的包装器--只提供特定函数集合.栈从被称作栈顶的容器尾部推弹元素. 一:头文件 #i ...
- FansUnion:共同写博客计划终究还是“流产”了
首先说说我原本的计划:我和周围的同学.朋友.好友 共同维护一个博客. 我对其他人并没有过高的期待.我一个人的写作量 = 其他人的写作量. 现实是,其他人没有怎么写. 对于,这个结果,我非常低无奈.谩骂 ...
- TensorFlow 制作自己的TFRecord数据集
官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而关于 ten ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第六篇【AppiumLibrary等待函数介绍】
http://blog.csdn.net/deadgrape/article/details/50622441 废话不多说,少年们请看下面. Wait Until Page Contains text ...
- Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate(转)
今天看了下Spring的源码——关于JDBC的"薄"封装,Spring 用一个Spring JDBC模板类来封装了繁琐的JDBC操作.下面仔细讲解一下Spring JDBC框架. ...
- 设计模式之二十:责任链模式(Chain of Responsibility)
感觉这个设计模式和组合模式一样是一种非常巧妙的设计模式,在须要使用它的地方假设不使用这样的设计模式代码会变的非常复杂,可是这样的设计模式的基本原理又是非常easy的. 责任链模式: 通过使多个对象都有 ...
- mysql点滴_02程序中运行sql语句报字符集问题解决
程序中运行 "SELECT t.EVENT_TYPE_ID FROM RATABLE_EVENT_TYPE t WHERE t.NAME='帐期末费用转移事件'" 报错 错误码 ...
- android优化 清除无效代码 UCDetector
android下优化 清除无效 未被使用的 代码 UCDetector 官方下载地址:http://www.ucdetector.org/index.html UCDetector 是 eclips ...
- 使用android.graphics.Path类自绘制PopupWindow背景
PopupWindow简单介绍 PopupWindow是悬浮在当前activity上的一个容器,用它能够展示随意的内容. PopupWindow跟位置有关的API有以下几个: showAsDropDo ...
- Bash脚本中的操作符
一.文件測试操作符 假设以下的条件成立将会返回真. -e 文件存在 -a 文件存在 这个选项的效果与-e同样. 可是它已经被"弃用"了, 而且不鼓舞使用. -f 表示这个文件是一个 ...