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

WA实在找不出哪里错了 ~~堆 大家帮我找找吧

Posted by nameisno at 2008-02-04 12:21:35 on Problem 3476
#include "iostream"
#include "string.h"
char str[1000100];
long heap[1000100][2];
long size;

using namespace std;

inline void swap(long A[][2],int x, int y){
	int t;
	t=A[x][0], A[x][0]=A[y][0], A[y][0]=t;
	t=A[x][1], A[x][1]=A[y][1], A[y][1]=t;
}

void heap_increase_key(long A[][2],int i,long key) {
	A[i][0]=key;
	while(i>1 && ( 
			A[i/2][0]<A[i][0] || 
			(A[i/2][0]==A[i][0] && A[i/2][1]>A[i][1]))
	){
		swap(A,i,i/2);
		i=i/2;
	}
}
void max_heapify(long A[][2],long n, int i) {//a[1]--a[n] 
	int l=i*2;
	int r=i*2+1;
	long largest;
	if(l<=n && (
			A[l][0]>A[i][0] || ( 
					A[l][0]==A[i][0] && A[l][1]<A[i][1]
			)
	)) largest=l;
	else largest=i;
	if(r<=n && (
			A[r][0]>A[largest][0] || (
					A[r][0]==A[largest][0] && A[r][1]<A[largest][1]
			)
	)) largest=r;
	if(i!=largest){
		swap(A,i,largest);
		max_heapify(A, n, largest);
	}
	return ;
}

void build_max_heap(long A[][2], long size) {
	int i;
	for(i=size; i>=1; i--) {
		max_heapify(A,size,i);
	}
	return ;
}

void max_heap_insert(long A[][2],long &size,long key[2]) {
	size++;
	A[size][0]=0;
	A[size][1]=key[1];
	heap_increase_key(A, size, key[0]);
}

int heap_extract_max(long A[][2], long &size) {
	char c=str[A[1][1]];
	int i;
	int key_i=-1;
	if(A[1][0]==1||size==0) return key_i;
	key_i=A[1][1];
	cout<<c;
	//printf("%c ",c);
	for(i=A[1][1]; A[1][0]!=0 ; i++) {
		if(str[i]==c) {
			cout<<" "<<i+1;
			//printf("%d ",i+1);
			str[i]='*';
			A[1][0]--;
		}
	}
	cout<<endl;

	swap(A,1,size);
	size--;
	max_heapify(A,size,1);
	return key_i;
}


void erase_from_heap(long A[][2], long &size, long i) {
	A[i][0]=A[size][0];
	A[i][1]=A[size][1];
	size--;
	max_heapify(A, size, i);
}

void max_heap_merger(long A[][2], long &size, int key_i) {
	int r,key_r;
	int l,key_l;
	key_r=strlen(str);
	
	key_l=-1;
	l=0;
	r=0;
	int i;
	for(i=1; i<=size; i++) {
		if(str[A[i][1]] != '*') {
			if(A[i][1]>key_l && A[i][1]<key_i) {
				key_l = A[i][1];
				l=i;
			}
			if(A[i][1]>key_i && A[i][1]<key_r) {
				key_r = A[i][1];
				r=i;
			}
		}
	}
	
	if(l==0 || r==0 || str[key_l]!=str[key_r]) return ;
	long key[2];
	key[0]=A[l][0]+A[r][0];
	key[1]=A[l][1];
	
	erase_from_heap(A, size, l);

	for(i=1; i<=size; i++) if(A[i][1]==key_r) break;
	erase_from_heap(A, size, i);
	
	max_heap_insert(A, size, key);
	return ;
}



int input(long A[][2]) {
	cin.getline(str, 1000000);
	
	//scanf("%s",str);
	int size=1;
	int i;
	char c;
	A[size][0]=1;
	A[size][1]=0;
	c=str[0];
	for(i=1; str[i]!='\0'; i++) {
		if(c==str[i]) {
			A[size][0]++;
		} else {
			size++;
			A[size][0]=1;
			A[size][1]=i;
			c=str[i];
		}
	}
	return size;
} 


void sovle() {
	size=input(heap);

	build_max_heap(heap, size);

	int key_i;
	while((key_i=heap_extract_max(heap,size))!=-1) {
		max_heap_merger(heap,size, key_i);
		
	}
	return ;
}



int main() {
	//freopen("e:\\in.dat", "r", stdin);
	sovle();
	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