2012年华东交通大学“双基”ACM程序设计竞赛 1006排序

http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1006&cid=18128&hide=0

题目叫排序,这题排序真的可以做~

思路是这样的:假如是一个长串,一个短串,从前往后,长串消去连续短串后,两个串字典序比较大小,

比如说,101210121,和1012,最后比较的串是1和1012

若是556和443,最后比较的串是556和443

若是1012,10121012,那么这两个串视为相等的哦,因为最后比较大小的串是0,0

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct baby
{
    char s[30];
    int len;
} re[1005];
bool cmp1(baby a,baby b)
{
    int i,j;
    if(a.len>=b.len)
    {
        for(i=0; i<a.len; i+=j)
        {
            for(j=0; j<b.len&&i+j<a.len; ++j)
            {
                if(a.s[i+j]==b.s[j])
                    continue;
                else return a.s[i+j]<b.s[j];
            }
        }
        return true;
    }
    for(i=0; i<b.len; i+=j)
    {
        for(j=0; j<a.len&&i+j<b.len; ++j)
        {
            if(b.s[i+j]==a.s[j])
                continue;
            else return a.s[j]<b.s[j+i];
        }
    }
    return false;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; ++i)
        {
            scanf("%s",re[i].s);
            re[i].len=strlen(re[i].s);
        }
        sort(re,re+n,cmp1);
        for(int i=0; i<n; ++i)
            printf("%s",re[i].s);
        puts("");
    }
}

 

posted @ 2012-12-05 22:40  小仪在努力~  阅读(137)  评论(0)    收藏  举报