Usaco contact, puzzle, operator overloading, QuickSort,

Source: Internet
Author: User

I feel like the hardest question so far
reference to the vanishing Person's code: Http://www.nocow.cn/index.php/Code:USACO/contact/C%2B%2B
Reference ideas: http:// www.nocow.cn/index.php/USACO/contact#. E7. ae.80.e6.98.93.e4.bd.86.e9.9d.9e.e5.b8.b8.e9.ab.98.e6.95.88.e7.9a.84.e6.96.b9.e6.b3.95
A simple but very efficient way The Vanishing Person

/* Id:wangxin12 prog:contact lang:c++/#include <iostream> #include <vector> #include <fstream>
#include <string> #include <cstring> using namespace std;
#define MAX 999999999 int A, B, N;
int frequency[12][4100];
int transform, length;

int const_num[13] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};

Class Unit {Public:int frequency, length, num;};
Vector<unit> Unit_pool;

String original_string;
	void Create_unit (int length, int num) {unit * u = new unit ();
	U->frequency = Frequency[length-1][num];
	u->length = length;
	U->num = num;
Unit_pool.push_back (*u);
		BOOL Operator> (unit U1, unit U2) {if (U1.frequency < u2.frequency) | |
		(u1.frequency = = u2.frequency && u1.length > u2.length) | |
		(u1.frequency = u2.frequency && u1.length = u2.length && u1.num > U2.num))
	return true;
else return false; } template<typename t> int partition (vector<t> &f, int start, int end) {T key = F[start];
	int left = start + 1, right = end;
		while (true) {while (left < right && F[right] > key) right--;
		while (left < right && key > F[left]) left++;
		if (left = right) break;
		T tmp = F[left];
		F[left] = F[right];
	F[right] = tmp;
	} if (F[left] > key) return start;
	F[start] = F[left];
	F[left] = key;
return to left;
	} template<typename t> void Quick_sort (vector<t> &f, int start, int end) {if (start >=) return;
	int boundary = partition (f, start, end);
	Quick_sort (f, start, boundary-1);
Quick_sort (F, boundary + 1, end);
	string transform_to_binary (int num, int length) {string S1 = "", S2 = "";
	int t = num;
	char tmp;
		while (t!= 0) {S1 + = (t% 2 + ' 0 ');
	T/= 2;
	for (int j = s1.length ()-1; J >= 0; j--) s2 + = S1[j];
	S1 = "";
	for (int i = 1; I <= length-s2.length (); i++) S1 + = ' 0 ';
	S1 = s1 + s2;
return s1;
int main () {ifstream fin ("contact.in");	Ofstream fout ("Contact.out");
	fin>>a>>b>>n;
	Original_string want a char to read the char, otherwise the char ch is stopped by reading the newline character;
	while (Fin.get (CH)) {if (ch >= ' 0 ' && ch <= ' 1 ') original_string + = ch; //for (length = A; (length <= B) && (length <= original_string.size ());
			length++) {for (int start = 0; start <= original_string.length ()-length; start++) {transform = 0; for (int j = start; J <= start + length-1 j + +)//for Transform chronicle string size if (original_string[j] = = ' 1 ') transfor
			M + + Const_num[start + length-j-1];
		frequency[length-1][transform]++; for (int i = A; I <= B. i++) {for (int j = 0; J < Const_num[i]; J +) {if (frequency[i-1][j)!= 0) C
		Reate_unit (i, j);

	} quick_sort (Unit_pool, 0, (int) unit_pool.size ()-1);
	int last_frequency = 99999999, sum = 0, count = 0;
			for (int k = 0; k < unit_pool.size (); k++) {if (Unit_pool[k].frequency < last_frequency) {sum++; if (sum = =N + 1) break;
			if (k!= 0) fout<<endl;
			Count = 1;
			fout<<unit_pool[k].frequency<<endl;
			Fout<<transform_to_binary (Unit_pool[k].num, unit_pool[k].length);
		Last_frequency = unit_pool[k].frequency;
			else {count++;
				if (count = = 7) {fout<<endl;
			Count = 1;
			else fout<< "";
		Fout<<transform_to_binary (unit_pool[k].num,unit_pool[k].length);

	}} fout<<endl;
	Fin.close ();

	Fout.close ();
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.