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

确实,太烦人了,附代妈&512题纪念

Posted by KatrineYang at 2016-11-13 14:15:33 on Problem 1521 and last updated at 2016-11-13 14:16:39
In Reply To:Re:一定注意输入只有一个字符的情况,因为这个跪了三次 Posted by:RiseFalcon at 2016-10-16 07:42:56
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

struct node{
	node *left;
	node *right;
	char c;
	int freq;
}nodes[233];

struct nodep{
	node* p;
	nodep(node* p):p(p){}
};

bool cmp(node *n1, node *n2){
	return n1->freq > n2->freq;
}

bool operator<(const nodep& n1, const nodep& n2){
	return cmp(n1.p, n2.p);
}

void collect(node* root, int* col, int depth){
	//cout << "depth: " << depth << endl;
	if(root->left == NULL && root->right == NULL){
		//cout << depth << " " << root->freq << endl;
		*col += depth * root->freq;
		return;
	}
	collect(root->left, col, depth+1);
	collect(root->right, col, depth+1);
}

void ent(string s){
	int l = s.length();
	bool used[233] = {0};
	for(int i = 0; i < l; i++){
		int idx = (int)s[i];
		if(!used[idx]){
			used[idx] = 1;
			nodes[idx].c = s[i];
			nodes[idx].freq = 1;
			nodes[idx].left = NULL;
			nodes[idx].right = NULL;
		}
		else{
			nodes[idx].freq++;
		}
	}
	priority_queue<nodep> pq;
	for(char c = 'A'; c <= '_'; c++){
		if(used[(int)c]) pq.push(nodep(&nodes[(int)c]));
	}
	if(pq.size() == 1){
		printf("%d %d 8.0\n", 8*l, l);
		return;
	}
	//printf("pqsize: %d\n", pq.size());
	while(pq.size() > 1){
		node* n1 = pq.top().p;
		pq.pop();
		node* n2 = pq.top().p;
		pq.pop();
		node* t = new node();
		t->left = n1, t->right = n2, t->freq = n1->freq+n2->freq;
		pq.push(nodep(t));
	}
	node *root = pq.top().p;
	int col = 0;
	collect(root, &col, 0);
	printf("%d %d %.1lf\n", 8*l, col, 8.0*l/col);
}

int main() {
	while(1){
		string s;
		while(s.length()==0) getline(cin, s);
		if(strcmp(s.c_str(), "END")==0) break;
		ent(s);
	}
	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