2435: Dictionary Order

本文介绍了一种用于对英语单词进行特殊排序的算法。该算法首先比较单词长度,短的优先;若长度相同,则按字母从'a'到'z'的位置进行逐个比较;最后,在以上规则无法区分的情况下采用常规字典顺序进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 2435: Dictionary Order


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s32768K825146Standard

One day, skywind will build a new dictionary includes a lot of english words. In this dictionary, one word is standing in the front of other word if these conform to the following principles: 1. The shorter word is better and ahead. 2. From letter 'a' to 'z', Let xi and yi is the first place of letter 'a' or 'A' in each word (it is infinity if this letter can't be find), the smaller is better. If they are same, continue the next letter until 'z' or 'Z'. 3. Compare using common dictionary order if they are not comparable as above.

Input

The first line of each case is a integer n (n is less than 1000) who is the number of words. The input is finished if n is zero. There is just one word in the next n lines. The word consists of only lowercase and uppercase letter, its length is no more than 20.

Output

Output the dictionary order by the above rules. Print one word in one line. Each case should be separated from the previous by one blank line.

Sample Input

5
Abovee
Sample
Input
AND
Output
0

Sample Output

AND
Input
Abovee
Sample
Output

 

Problem Source: provided by skywind

 


This problem is used for contest: 94 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char s[21];
}str;

int pos(char s[], char a) {
    int pos = 50;
    for(int i = 0; s[i] != '/0'; i++){
        if(s[i] == a){
            pos = i;
            break;
        }
    }
    return pos;
}

int cmp ( const void *a , const void *b ) {
    if( strlen((*(str *)a).s) != strlen((*(str *)b).s) )
        return strlen((*(str *)a).s) - strlen((*(str *)b).s);
    else {
        char s1[21], s2[21];
        int i;
        strcpy(s1, (*(str *)a).s);
        strcpy(s2, (*(str *)b).s);
        for(i = 0; i < strlen(s1); i++){
            if(s1[i] >= 'A' && s1[i] <= 'Z')
                s1[i] = s1[i] - 'A' + 'a';
        }
        for(i = 0; i < strlen(s2); i++){
            if(s2[i] >= 'A' && s2[i] <= 'Z')
                s2[i] = s2[i] - 'A' + 'a';
        }
        for(i = 'a'; i <= 'z'; i++){
            if(pos(s1, i) < pos(s2, i)){
                return -1;
              //  break;
            }
            if(pos(s1, i) > pos(s2, i)){
                return 1;
             //   break;
            }
        }
        if(i == 'z' + 1){
            if( strcmp((*(str *)a).s, (*(str *)b).s) >0 )
                return 1;
            else
                return 0;
        }
    }
}

int main () {
    int n,i;
    str ss[1000];

    scanf("%d", &n);
    if(n == 0)
        goto end;
    for(i = 0; i < n; i++)
        scanf("%s", ss[i].s);
    qsort(ss, n, sizeof(ss[0]), cmp);
    for(i = 0; i < n; i++)
        printf("%s/n", ss[i].s);

    while(scanf("%d",&n) && n != 0){
        for(i = 0; i < n; i++)
            scanf("%s", ss[i].s);
        qsort(ss, n, sizeof(ss[0]), cmp);
          printf("/n");
        for(i = 0; i < n; i++)
            printf("%s/n", ss[i].s);
    }
    end:;
    return 0;
}

 

 

 

#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;
struct node
{
    string str,s;
    int size;
};
node a[1001];
int pos(string s,char ch)
{
    int p=50;
    for(int i=0;i<s.size();i++)  if(s[i]==ch) {  p=i;  break;}
    return p;
}
bool cmp(node x,node y)
{
   if(x.size==y.size)
   {
       string s1=x.s,s2=y.s;int i;
       for( i='a';i<='z';i++)
       {
           if(pos(s1,i)!=pos(s2,i))
           {
                return pos(s1,i)<pos(s2,i);
           }
       }
       if(i=='z'+1) return x.str<y.str;
   }
   return x.size<y.size;
}
int main()
{
    int n,i,j;
    int f=0;
    while(scanf("%d",&n)==1&&n)
    {
        if(f==0) f=1;else printf("/n");
        for(int i=0;i<n;i++)
        {
            cin>>a[i].str;a[i].s=a[i].str;
            a[i].size=a[i].str.size();
            for(int j=0;j<a[i].size;j++) a[i].s[j]=tolower(a[i].s[j]);
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++) cout<<a[i].str<<endl;
    }
    return 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值