UVa 1584-circular Sequence

Source: Internet
Author: User

Https://uva.onlinejudge.org/external/15/1584.pdf

Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence
CGAGTCAGCT", that is, the last symbol"T" inCGAGTCAGCT" is connected to the first symbol"C".
We always read a circular sequence in the clockwise direction.
Since It is isn't easy to store a circular sequence in a computer as it's, we decided to store it as a linear
Sequence. However, there can be many linear sequences that is obtained from a circular sequence by cutting
Any place of the circular sequence. Hence, we also decided to store the linear sequence
lexicographically smallest among all linear sequences the can is obtained from a circular sequence.
Your task is to find the lexicographically smallest sequence from a given circular sequence. For the example
In the figure, the lexicographically smallest sequence is "AGCTCGAGTC". If there is, or more linear
Sequences that is lexicographically smallest, you is to find all one of the them (in fact, they is the same).
Input
The input consists of T test cases. The number of test cases T is given on the first line of the input file. each
Test case takes-one line containing a circular sequence that's written as an arbitrary linear sequence. Since the
Circular sequences is DNA sequences, only four symbols, A, C, G and T, is allowed. Each sequence have
Length at least 2 and at the most 100.
Output
Print exactly one line for each test case. The contain the lexicographically smallest sequence for the
Test case.
The following shows sample input and output for both test cases.
Sample Input
2
Cgagtcagct
CTCC
Sample Output
Agctcgagtc
CCCT

Analysis:
Give you a string, this string is a ring, you need to find this ring in the dictionary order the smallest string, and output can

#include <iostream>#include <sstream>#include <iomanip>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <bitset>#include <string>#include <numeric>#include <algorithm>#include <functional>#include <iterator>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <cctype>#include <complex>#include <ctime>#define INF 0x3f3f3f3f#define EPS 1e-6#define P (x) printf ("%d\n", x)#define K (x) printf ("Case%d:", ++x)#define MES (x, D) memset (x, D, sizeof (x))#define S (x) scanf ("%d", &x)typedef Long LongLL;Const DoublePI =ACOs(-1.0);Const Long LongMoD =1e9+7;using namespace STD;Chars[ the];intLess (Const Char* S,intPintQ) {intLen =strlen(s); for(inti =0; i < len;i++)if(s[(i + p)% len]! = s[(i + q)% len])returns[(p + i)% len] < s[(i + q)% len];return 0;}intMain () {//freopen ("Int.txt", "R", stdin);    //freopen ("OUT.txt", "w", stdout);    intT,n;scanf("%d", &t); while(t--) {scanf('%s ', s);intAns =0;intLen =strlen(s); for(inti =0; i < len;i++) {if(Less (S,i,ans)) ans = i; } for(inti =0; i < len;i++)Putchar(s[(i + ans)% len]);puts(""); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVa 1584-circular Sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.