gdufe acm 1014 Scramble Sort

题目链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值