Scramble Sort |
Problem description |
In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word. |
Input |
The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single period. |
Output |
For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period. |
Sample Input |
0. banana, strawberry, OrAnGe. Banana, StRaWbErRy, orange. 10, 8, 6, 4, 2, 0. x, 30, -20, z, 1000, 1, Y. 50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle. . |
Sample Output |
0. banana, OrAnGe, strawberry. Banana, orange, StRaWbErRy. 0, 2, 4, 6, 8, 10. x, -20, 1, Y, 30, 1000, z. -100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm. |
User: cspark , Language : GNU C++ , Judge Result: Accepted
#include<iostream> #include<algorithm> #include<string> using namespace std; bool cmp (string a, string b) { int i; string s1=a; string s2=b; for(i=0;i<s1.length();++i) { if(s1[i]>='a'&&s1[i]<='z') s1[i]=s1[i]-'a'+'A'; } for(i=0;i<s2.length();++i) { if(s2[i]>='a'&&s2[i]<='z') s2[i]=s2[i]-'a'+'A'; } int length=s1.length()>s2.length()?s1.length():s2.length(); for(i=0;i<length;++i) { if(s1[i]!=s2[i]) return s1[i]<s2[i]; } return false; } int main() { string s; string str[1000]; int num[1000]; int flag[2000]; int i,f,len,s_len=0,n_len=0,index=0,fla=0; memset(flag,0,sizeof(flag)); while(cin>>s&&s!=".") { len = s.length()-1; if(s[len]=='.') fla=1; for(i=0,f=0;i<s.length();++i) { if ((s[i] >= 'A'&&s[i] <='Z')||(s[i]>= 'a'&&s[i] <= 'z')) { f=1; s=s.erase(len,1); str[s_len++]=s; break; } } if(!f) { int tmp=0,k=0; if(s[0]=='+') continue; if(s[0]=='-') k=1; for(int p=k ;p<=len-1;++p) { tmp *= 10; tmp += s[p] - 48; } if(k) tmp = -tmp; num[n_len++]=tmp; flag[index]=1; } ++index; if(fla) { sort(num,num+n_len); sort(str,str+s_len,cmp); int q=-1,k1=0,k2=0; while( 1 ) { ++q; if( q >= (n_len+s_len-1)) break; if(k1>=n_len) { cout<<str[k2++]<<", "; continue; } if(k2>=s_len) { cout<<num[k1++]<<", "; continue; } if(flag[q]&&k1<n_len) cout<<num[k1++]<<", "; else cout<<str[k2++]<<", "; } if(k1!=n_len) cout<<num[k1]<<"."; else cout<<str[k2]<<"."; cout<<endl; s_len=0;n_len=0; memset(flag,0,sizeof(flag)); index=0; fla=0; } } return 0; }