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

忘了空格 搞了一次PE 真是不该

Posted by KatrineYang at 2016-11-10 09:39:55 on Problem 1577
#include <iostream>
#include <string>
using namespace std;

const int mx = -1;
int depth[26];

void init(){
	for(int i = 0; i < 26; i++) depth[i] = mx;
}

struct node{
	int val;
	node *left, *right;
};

int lgIdx(int l, int u){
	int m=-1, v=mx;
	for(int i = l; i <= u; i++){
		if(depth[i] > v){
			m=i;
			v=depth[i];
		}
	}
	return m;
}

void buildTree(node *root, int l, int u, int m){
	root->val = m;
	int lv, rv;
	if((lv = lgIdx(l, m-1)) != -1){
		root->left = new node();
		buildTree(root->left, l, m-1, lv);
	}
	else{
		root->left = NULL;
	}
	if((rv = lgIdx(m+1, u)) != -1){
		root->right = new node();
		buildTree(root->right, m+1, u, rv);
	}
	else{
		root->right = NULL;
	}
}

void visitTree(node *root){
	if(root == NULL) return;
	cout << (char)(root->val+'A');
	visitTree(root->left);
	visitTree(root->right);
}

int main() {
	while(1){
		bool toBreak = 0;
		init();
		int dpth = 0;
		while(1){
			string s;
			getline(cin, s);
			if(s[0] < 'A' || s[0] > 'Z'){
				if(s[0]=='$') toBreak=1;
				break;
			}
			else{
				int l = s.length();
				for(int i = 0; i < l; i++){
					depth[s[i]-'A'] = dpth;
				}
			}
			dpth++;
		}
		node *root = new node();
		buildTree(root, 0, 25, lgIdx(0,25));
		visitTree(root);
		cout << endl;
		if(toBreak) break;
	}
	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