HDU 3293
注意按字典序的时候比较所有字母。
#include<iostream> #include<string> #include<string.h> #include<stdio.h> #include<math.h> #include<queue> #include<algorithm> using namespace std; struct node{ char name[22]; char origin[22] ; char level [22]; int lev; }tt[510]; int cmp(node a,node b){ if(strcmp(a.origin,b.origin)==0){ if(strcmp(a.level,b.level)==0){ int len=max(strlen(a.name),strlen(b.name)); int i=0; while(i<len){ if(a.name[i]==b.name[i]){ i++; }else{ return a.name[i] < b.name[i]; } } }else{ return a.lev > b.lev; } }else{ int len=max(strlen(a.origin),strlen(b.origin)); int i=0; while(i<len){ if(a.origin[i] == b.origin[i]){ i++; } else { return a.origin[i] < b.origin[i];} } } } int main(){ int n,num=0; while(cin>>n){ num++; for(int i=0;i<n;i++){ cin>>tt[i].name>>tt[i].origin>>tt[i].level ; if(tt[i].level[0]=='w'){ tt[i].lev=3; }else if(tt[i].level[0]=='g'){ tt[i].lev=2; }else{ tt[i].lev=1; } } string cc; sort(tt,tt+n,cmp); cout<<"Case "<<num<<endl; for(int i=0;i<n;i++){ if(cc!=tt[i].origin){ cout<<tt[i].origin<<":"<<endl; cc=tt[i].origin; } cout<<" "<<tt[i].name<<" "<<tt[i].level<<endl; } } return 0; }