2435: Dictionary Order
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 32768K | 825 | 146 | Standard |
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;
}
本文介绍了一种用于对英语单词进行特殊排序的算法。该算法首先比较单词长度,短的优先;若长度相同,则按字母从'a'到'z'的位置进行逐个比较;最后,在以上规则无法区分的情况下采用常规字典顺序进行排序。
3万+

被折叠的 条评论
为什么被折叠?



