文章
454
粉丝
0
获赞
1
访问
10.1k
每组先给个数,再读若干个字符串(读到 stop 或读满个数为止),按长度从小到大排序输出。
读第一行个数 n,然后循环读行;若读到 stop 立即结束本组(stop 不输出),否则存入并按数量上限截断。
按字符串长度升序排序;长度相同的按输入先后保持稳定(用稳定排序即可)。
多组数据,一组结束后继续读下一组的个数直到 EOF。
时间 O(n log n),空间 O(n)。
C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char all[200005];
static char *ln[4000];
int main(void){
int L=0,c;while((c=getchar())!=EOF&&L<200000)all[L++]=(char)(c=='\r'?'\n':c);
all[L]=0;
int nl=0;char*p=all;
ln[nl++]=p;
for(;*p;p++)if(*p=='\n'){*p=0;ln[nl++]=p+1;}
int idx=0;
while(idx<nl){
while(idx<nl&&ln[idx][0]==0)idx++;
if(idx>=nl)break;
int cnt=atoi(ln[idx]);idx++;
char buf[4000][128];int bc=0,i,j;
for(i=0;i<cnt;i++){
while(idx<nl&&ln[idx][0]==0)idx++;
if(idx>=nl)break;
char *s=ln[idx++];
if(strcmp(s,"stop")==0)break;
strcpy(buf[bc++],s);}
for(i=1;i<bc;i++){char v[128];strcpy(v,buf[i]);int j2=i;
while(j2>0){int la=(int)strlen(buf[j2-1]),lb=(int)strlen(v);
if(la<...
登录后发布评论
暂无评论,来抢沙发