就是统计字符串每一个字符后面比这一字符小的数目;
链接 http://poj.org/problem?id=1007
第一次是用冒泡做的 ac 因为题目中有这样一句话“Since two strings can be equally sorted, then output them according to the orginal order.”
之后我又贼心不死,又用了快排试了一下,wa 果然。。。
代码:
#include <stdio.h>
#include <string.h>
typedef struct{
char s[55];
int sor;
}st;
int main()
{
st str[105];
int n, m, i, flag;
scanf( "%d%d", &n, &m );
for( i = 0; i < m; i ++ ){
scanf( "%s", str[i].s );
str[i].sor = 0;
for( int j = 0; j < n-1; ++ j ){
if( str[i].s[j] != 'A' )
for( int k = j+1; k < n; ++ k ){
if( str[i].s[j] > str[i].s[k] )
++str[i].sor;
}
}
}
char temp[55];
for( i = 0; i < m-1; i ++ )
for( int j = 0; j < m-1-i; ++ j )
if( str[j].sor > str[j+1].sor ){
strcpy( temp, str[j].s );
strcpy( str[j].s, str[j+1].s );
strcpy( str[j+1].s, temp );
int t = str[j].sor;
str[j].sor = str[j+1].sor;
str[j+1].sor = t;
}
for( i = 0; i < m; ++ i )
printf( "%s\n", str[i].s );
return 0;
}