注册 登录
编程论坛 C++教室

1道题,不知道那里错啦 求大神分析

lishenming 发布于 2012-10-14 18:54, 669 次点击
http://acm.whu.
这是题目链接

下面是我写的代码,不知道哪里出错啦,求帮忙看看!!!
程序代码:
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char a[101];

    while (cin>>a)
    {
        int count = 0;
        int i,j,m,n,x;
        char b[101][101] = {0};
        while (a[count] != '\0')
        {
            count++;
        }
        for (i=0; i<count; i++)
        {
            for (j=0; j<count; j++)
            {
                b[j][(j+i+count)%count] = a[i];
            }

        }
        for (m=0; m<count-1; m++)
        {
            if (strcmp(b[m], b[m+1]) < 0)
            {
                char temp[101];
                strcpy(temp, b[m]);
                strcpy(b[m], b[m+1]);
                strcpy(b[m+1], temp);
            }

        }
        for (n=0; n<count; n++)
        {
            if (strcmp(b[n], b[count-1]) == 0)
            {
               cout<<b[n]<<endl;
            }
        }
    }

    return 0;
}

8 回复
#2
lz10919149992012-10-14 20:06
程序代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int compare(const void* str1, const void* str2)
{
    return strcmp((const char*)str1, (const char*)str2);
}

int main()
{
    char bracelet[101];
    char strings[100][101];
    int length, i, j;
    while (cin >> bracelet) {
        length = strlen(bracelet);
        for (i = 0; i < length; ++i) {
            for (j = 0; j < length; ++j)
                strings[i][j] = bracelet[(i+j)%length];
            strings[i][j] = '\0';
        }
        qsort(strings, length, sizeof *strings, compare);
        cout << strings[0] << endl;
    }
}
#3
lishenming2012-10-15 10:27
回复 2楼 lz1091914999
他不是要求输出各种可能的情况么?你这应该只能输出一种吧??

还有我的代码错在哪里啊??

麻烦你啦 谢谢
#4
lishenming2012-10-16 11:09
怎么没有人啊 。。。
#5
liman1232012-10-16 12:56
建议把错误信息贴一下,哪儿错了?
#6
lishenming2012-10-17 13:19
回复 5楼 liman123
我自己编译是没有错误的  只是系统说我答案错误 。。。

我也不知道错在哪里。。
#7
cj6572064272012-10-17 13:39
你那个链接打不开,能不能把题目在这里面贴上啊
#8
lishenming2012-10-17 14:07
回复 7楼 cj657206427
DescriptionSome beads are embedded in a bracelet, and each beads is carved with a lower case letter, as the follow figure shows:
 
If we read the letters in counter clockwise with different initial position, we may get different strings. The above example, we can read 6 strings:
acabdb
cabdba
abdbac
bdbaca
dbacab
bacabd
Sort these strings in lexical order, we obtain:
abdbac
acabdb
bacabd
bdbaca
cabdba
dbacab
What we need is the first string: abdbac.

InputA
string of lower case letters representing the letters carved on the bracelet (in counter clockwise order).
The length of the string is no more than 100.OutputOutput the first string of all the possible strings after sorting them in lexical order.Sample

Input

acabdb
kkisverystupid
Sample Output

abdbacd
kkisverystupi
#9
rjsp2012-10-18 08:44
先来个不必拷贝内存,不受100长度限制的
程序代码:
#include <stdio.h>
#include <string.h>

int compare( const char* str, size_t n, size_t a, size_t b )
{
    if( a == b ) return 0;
    if( a < b ) return -compare(str,n,b,a);

    int r = memcmp( str+a, str+b, n-a );
    if( r ) return r;

    r = memcmp( str+0, str+b+n-a, a-b );
    if( r ) return r;

    r = memcmp( str+a-b, str+0, b );
    return r;
}

int foo( const char* str )
{
    int r = 0;
    size_t n = strlen( str );
    for( size_t i=1; i<n; ++i )
    {
        if( compare(str,n,i,r) < 0 )
            r = i;
    }

    printf( "%s%.*s\n", str+r, r, str );
    return r;
}

int main()
{
    foo( "acabdb" ); // abdbac
    foo( "kkisverystupid" ); // dkkisverystupi

    return 0;
}

如果看不懂,来个简单的
程序代码:
#include <stdio.h>
#include <string.h>

int foo( const char* str )
{
    char buf[201];
    size_t n = strlen( str );
    strcpy( buf, str );
    strcpy( buf+n, str );

    int r = 0;
    for( size_t i=1; i<n; ++i )
    {
        if( strncmp(str+i,str+r,n) < 0 )
            r = i;
    }

    printf( "%s%.*s\n", str+r, r, str );
    return r;
}

int main()
{
    foo( "acabdb" ); // abdbac
    foo( "kkisverystupid" ); // dkkisverystupi

    return 0;
}

1