Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

代妈

Posted by KatrineYang at 2016-11-13 14:39:56 on Problem 1520
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

struct wordt{
	string word;
	string upword;
}words[10000];

int numCnt, wordCnt, totalCnt;
bool isNum[10000];
int nums[10000];

void init(){
	numCnt=0;
	wordCnt=0;
	totalCnt=0;
}

bool isaNum(string s){
	int l = s.length();
	for(int i = 0; i < l; i++){
		if((s[i]>'9' || s[i]<'0') && s[i] != '-') return 0;
	}
	return 1;
}

char up(char c){
	if(c>='a' && c<='z') return c-'a'+'A';
	return c;
}

string up(string s){
	string res;
	int l = s.length();
	for(int i = 0; i < l; i++){
		res += up(s[i]);
	}
	return res;
}

bool cmp(const wordt& s1, const wordt& s2){
	return strcmp(s1.upword.c_str(), s2.upword.c_str())==-1;
}

void parse(string s){
	int l = s.length();
	int start = 0;
	for(int i = 0; i < l; i++){
		if(s[i] == ',' || s[i] == '.'){
			string xc = s.substr(start, i-start);
			if(xc[0] == ' '){
				xc = xc.substr(1);
			}
			if(isaNum(xc)){
				isNum[totalCnt] = 1;
				nums[numCnt] = atoi(xc.c_str());
				numCnt++;
			}
			else{
				isNum[totalCnt] = 0;
				words[wordCnt].word = xc;
				words[wordCnt].upword = up(xc);
				wordCnt++;
			}
			start = i+1;
			totalCnt++;
		}
	}
}

int main() {
	string s;
	while(1){
		getline(cin ,s);
		if(s[0]=='.')break;
		init();
		parse(s);
		sort(words, words+wordCnt, cmp);
		sort(nums, nums+numCnt);
		int nword = 0, nnum = 0;
		for(int i = 0; i < totalCnt; i++){
			if(isNum[i]){
				printf("%d", nums[nnum]);
				nnum++;
			}
			else{
				printf("%s", words[nword].word.c_str());
				nword++;
			}
			printf("%s", i==totalCnt-1? ".\n": ", ");
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator