| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 669 人关注过本帖
标题:1道题,不知道那里错啦 求大神分析
只看楼主 加入收藏
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
1道题,不知道那里错啦 求大神分析
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;
}

搜索更多相关主题的帖子: 链接 
2012-10-14 18:54
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:7 
程序代码:
#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;
    }
}

My life is brilliant
2012-10-14 20:06
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
收藏
得分:0 
回复 2楼 lz1091914999
他不是要求输出各种可能的情况么?你这应该只能输出一种吧??

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

麻烦你啦 谢谢
2012-10-15 10:27
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
收藏
得分:0 
怎么没有人啊 。。。
2012-10-16 11:09
liman123
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:79
专家分:178
注 册:2012-10-6
收藏
得分:7 
建议把错误信息贴一下,哪儿错了?
2012-10-16 12:56
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
收藏
得分:0 
回复 5楼 liman123
我自己编译是没有错误的  只是系统说我答案错误 。。。

我也不知道错在哪里。。
2012-10-17 13:19
cj657206427
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-17
收藏
得分:0 
你那个链接打不开,能不能把题目在这里面贴上啊
2012-10-17 13:39
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
收藏
得分:0 
回复 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
2012-10-17 14:07
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
先来个不必拷贝内存,不受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;
}

2012-10-18 08:44
快速回复:1道题,不知道那里错啦 求大神分析
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023427 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved