acm-ACM的小伙伴进,uva-1584有些小问题求大神解惑

问题描述

ACM的小伙伴进,uva-1584有些小问题求大神解惑

Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence CGAGTCAGCT", that is, the last symbolT" in CGAGTCAGCT" is connected to the first symbolC". We always read a circular sequence in the clockwise direction.

epsfbox{p3225.eps}
Since it is not easy to store a circular sequence in a computer as it is, we decided to store it as a linear sequence. However, there can be many linear sequences that are obtained 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 find the lexicographically smallest sequence from a given circular sequence. For the example in the figure, the lexicographically smallest sequence is ``AGCTCGAGTC". If there are two or more linear sequences that are lexicographically smallest, you are to find 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 first line of the input file. 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.

The following shows sample input and output for two test cases.

Sample Input

2

CGAGTCAGCT

CTCC
Sample Output

AGCTCGAGTC
CCCT

我的代码如下

#include <iostream>
#include <cstring>
using namespace std;
int small(const char* s,int p,int q);
#define maxn 105
int main()
{
    int n;//测试数
    cin>>n;
    while(n--){
        //为何不能用char *str={0};?
        char str[maxn];
        cin>>str;
        int len=strlen(str);
        int ans=0;
        for(int i=1;i<len;i++){
            if(small(str,i,ans))
                ans=i;
        }
        for(int i=0;i<len;i++){
            putchar(str[(ans+i)%len]);
        }
        putchar('n');
    }
    return 0;

}

int small(const char* s,int p,int q){  //比较表示法p和表示法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;

}

问题疑惑在注释里,如果用注释里的会报空指针错误( returned -1073741819 (0xC0000005))问的问题很小白,让各位见笑了囧。

解决方案

 char *str={0};
  cin>>str;

这样写,你如何让机器知道str的大小?要知道下面还有输入语句cin的呀,cin会根据str的大小来选择性输入的,所以你这样写当然会报错。
你可以改成下面这样,指定str的大小,然后再输入的话就没问题了

 char *str = new char[maxn];
 cin>>str;
时间: 2024-10-02 17:56:54

acm-ACM的小伙伴进,uva-1584有些小问题求大神解惑的相关文章

acm-刷ACM的小伙伴进uva oj 455 Periodic Strings,求大神指出我的问题

问题描述 刷ACM的小伙伴进uva oj 455 Periodic Strings,求大神指出我的问题 Periodic Strings A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example the string ""abcabcabcabc&qu

uva-刷ACM小伙伴进Uva oj 340 - Master-Mind Hints

问题描述 刷ACM小伙伴进Uva oj 340 - Master-Mind Hints 如题: Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginnin

acm背包问题求大神赐教

问题描述 acm背包问题求大神赐教 for(i = 1; i<=n; i++) { for(j = v; j>=c[i]; j--)//在这里,背包放入物品后,容量不断的减少,直到再也放不进了 { f[i][v]=max(f[i-1][v],f[i-1][v-c[i]]+w[i]); } } 请问 第一个循环 i =1 之后 第二个循环 一直在放编号为1的石头啊?这两个循环都是干嘛的?那个 f[i][v] 数组 从00到 iv 都变成什么样了?求大神赐教 解决方案 直接说题目是什么你这么问,没

字符-一道acm水题 all in all 一直找不出错误 求大神解答

问题描述 一道acm水题 all in all 一直找不出错误 求大神解答 描述字符串s和t均由字母组成,若在t中除去一些字母能够得到s,我们就说s是t的一个子串.比如abc就是acbefc的子串(acbefc去掉第二.第四.第五个字符后就得到abc)输入有若干组输入数据,每组一行,分别为字符串s和t,s与t之间用空格隔开输出对于一组s与t,若s是t的子串,则输出Yes,否则输出No 样例输入sequence subsequence abc acb VERDI vivaVittorioEmanu

c++问题-在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里。不胜感激!!!

问题描述 在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里.不胜感激!!! #include #include using namespace std; int main() { int T; int k,t=0; int i, j, n1, n2; char a[1010], b[1010], c[1015]; string d[20], e[20], f[20]; cin>>T; for(k=1; k<=T; k++) { cin>>a>>b; d[

acm水题 二叉树模拟 hdu5444,能想到的测试数据都测了还是WA,求大神

问题描述 acm水题 二叉树模拟 hdu5444,能想到的测试数据都测了还是WA,求大神 1)我的代码(题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5444,也有复制内容在代码下面) #include <iostream> #include <string.h> using namespace std; const int maxn=1010; char record[maxn]; int sum=0; struct tree{ i

ACM一道题一直WA,求大神找错

问题描述 ACM一道题一直WA,求大神找错 题目: wa的程序: 解决方案 你的程序现在的逻辑是输入一个字符串就去判断是否在数组中存在, 并将结果打印出来. 但是题目要求是输入一组字符串在遇到end时才将最终结果输出出来. 所以你的应该将结果先保存起来, 遇到end在将 结果一股脑输出出来

acm-一道ACM的题求大神们解答,C语言,谢谢!

问题描述 一道ACM的题求大神们解答,C语言,谢谢! [题目描述] FFF团成员自带这样一个属性:凭空变出火把与汽油,两者配合起来才能让FFF之火duang的一下烧起来,但是不同的火把与不同的汽油配合产生的火焰是不同的,现在有n种火把与n种汽油,已知每一种火把与每一种汽油配合时产生的火焰的旺盛程度,现在求怎样使得火把与汽油一一配对,产生最旺盛的火焰. [输入] 第一行为一个整数T,表示有T组数据 每组数据第一行为一个正整数n(2≤n≤30) 第二行开始一共有n行,每行为n个正整数,第i行第j个数

c++的问题-ACM北大POJ_1376代码提交一直WA,求大神看看哪里错了?呜呜

问题描述 ACM北大POJ_1376代码提交一直WA,求大神看看哪里错了?呜呜 #include #include #include using namespace std; bool Map[55][55]; bool vis[55][55][4]; //[4] 四个directions,坐标和方向都相同时不能同时经过该点两次: int M,N; bool flag=false; //找到路径置为true: typedef struct { int x,y; //坐标: int time; /