题目链接:gdu 1014
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.
本题用尽我毕生所学。。。
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1000;
char s[maxn];
int flag[maxn];
struct S{
string str;
S(){}
S(string s):str(s){}
bool operator < (const S & p)const{//忽略大小写的字典序,比如 aaa < Bbb
string s1 = str, s2 = p.str;
int len = min(s1.length(), s2.length());
for(int i = 0; i < s1.length(); i++){
if(s1[i] <= 'Z') s1[i] = char(s1[i] + 'a' - 'A');
}
for(int i = 0; i < s2.length(); i++){
if(s2[i] <= 'Z') s2[i] = char(s2[i] + 'a' - 'A');
}
//cout << s1 << endl << s2 << endl;
for(int i = 0; i < len; i++)
if(s1[i] != s2[i]) return (s1[i] - s2[i] > 0);
return s1.length() > s2.length();
}
};
priority_queue<S> dict;
priority_queue<int, vector<int>, greater<int> > num;
inline int f(string s){
int ans = 0;
int i = 0, fu = 1;
if(s[0] == '-'){
fu = -1;
i = 1;
}
for(; i < s.length(); i++)
ans = ans * 10 + s[i] - '0';
return fu * ans;
}
int main(){
while(gets(s)){
memset(flag, 0, sizeof(flag));
while(dict.size()) dict.pop();
while(num.size()) num.pop();
int cnt = 0;
int len = strlen(s);
if(len == 1 && s[0] == '.') break;
for(int i = 0; i < len; i++)
if(s[i] == ',' || s[i] == '.'){
s[i] = ' '; cnt++;
}
stringstream str;
str << s;
string word;
int t = 0;
S oo;
while(str >> word){
if(word[0] == '-' || ('0' <= word[0] && word[0] <= '9')){
num.push(f(word));
//cout << "insert " << f(word) << endl;
flag[t] = 1;
}
else {
oo = S(word);
dict.push(oo);
}
t++;
}
S tmp1;
int tmp2;
for(int i = 0; i < cnt; i++){
if(flag[i]){
tmp2 = num.top();
num.pop();
cout << tmp2;
}
else{
tmp1 = dict.top();
cout << tmp1.str;
dict.pop();
}
if(i != cnt - 1) printf(", ");
else printf(".\n");
}
}
return 0;
}