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 ...
随机推荐
- 用div布局,页面copyright部分始终居于
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 训练1-K
一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢? Input 输入数据有若干组,每组数据包含二个整数a,b(0<a<10000, 10<b< ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)
不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我) . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...
- nodejs-函数
使用表达式定义的函数要提到使用之前,要不然无法解析,自然的function xx(xx)不用,ECMAscript自动提前 with关键字 引入空间命令空间,然后可以直接使用里面的对象了 label标 ...
- HDU 4353
利用分式的性质可以很容易证明要求的是个三角形,这很简单.对于求三角形内的雷的个数,只需求出每条边上方有多少个雷,作一点运算即可.如 A,B,C(B是X轴坐标在中间的点),则AC(其上方的雷的个数)-A ...
- git-osc自己定义控件之:CircleImageView
git-osc自己定义控件之:CircleImageView 一.CircleImageView的使用 在项目中能够发现,用户的头像都是圆形的.感觉非常好奇,昨天最终发现了,原来是自定了一个Image ...
- httpurlconnection get String
public static String getJSON(String url, int timeout) { HttpURLConnection c = null; try { long reque ...
- 【Dubbo实战】基础学习篇(一)
Dubbo的简单介绍 是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000多个服务提供30多亿次訪问量支持.并被广泛应用于阿里巴巴集团的各成员网站. Dubbo是一个分布式服 ...
- iOS 8使用Touch ID进行身份认证
iOS 8的SDK开放了Touch ID的接口.从WWDC的视频中能够看到Touch ID应用在两个方面:用于Key Chain加密和用于授权.iOS 8正式版公布以后我们能够看到Evernote的i ...
- mybatis使用generator自己主动生成代码时的类型转换
使用mybatis的generator自己主动生成代码,可是oracle数据库中number(6,2)总是自己主动转成BigDecimal.我想要转成的是float类型 这样就写了一个类型转换器,须要 ...