FJNU.1741
Description 任给n个串,由小到大词典排序.
Input n string_1 …… string_n Output 有序的串,每个串占一行
Sample Input 3 abc aaa aa
Sample Output aa aaa abc My Program
#include
<
iostream
>
#include
<
string
.h
>
#define
N 50
#define
M 100
using
namespace
std;
void
Change(
char
a[M],
char
b[M])
...
{ char temp[M]; strcpy(temp,a); strcpy(a,b); strcpy(b,temp); }
void
SORT(
int
n,
char
str[N][M])
...
{ int l[N] = ... { 0 } ; int i,j,k,m,t,swap; for (i = 0 ;i < n;i ++ ) l[i] = strlen(str[i]); for (i = 0 ;i < n;i ++ ) ... { for (j = i + 1 ;j < n;j ++ ) ... { swap = 0 ; t = l[i] > l[j] ? l[j]:l[i]; for (k = 0 ;k < t;k ++ ) if (str[i][k] == str[j][k]) continue ; else ... { swap = 1 ; if (str[i][k] > str[j][k]) ... { Change(str[i],str[j]); t = l[i];l[i] = l[j];l[j] = t; } break ; } if ( ! swap && str[i][k - 1 ] == str[j][k - 1 ] && l[i] > l[j]) ... { Change(str[i],str[j]); t = l[i];l[i] = l[j];l[j] = t; } } } }
int
main()
...
{ int n; char str[N][M]; int i; cin >> n; for (i = 0 ;i < n;i ++ ) scanf( " %s " ,str[i]); SORT(n,str); for (i = 0 ;i < n;i ++ ) printf( " %s " ,str[i]); return 0 ; }
YOYO's Note: 先输入所有的串,然后按位逐个比较,如果都相同则位数少的一个放前面。 自己写的很差的sort……当时不会用STL……