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 huicpc26 at 2005-08-31 23:45:56 on Problem 1767
#include <iostream>
#include <string>
#include <stack>
using namespace std;

long NextTreeIdentify(string t)
{
	string s,t2,t001("001");
	s = t;
	long res;
	int node,i,posof0,posof001;
	bool end = true;
	for(i=0;i<=s.size()-3;i++)
	{
		t2 = s.substr(i,3);
		if(t2.compare(t001)==0)
		{
			posof001 = i;
			break;
		}
	}
	end = true;
	for(i=posof001+3;i<s.size();i++)
	{
		if(s[i] != '1')
		{
			end = false;
			break;
		}
	}

	if(!end)
	{
		/*
		s.erase(posof001+1,2);
		posof0 = s.find_first_of("0",posof001+1);
		s.insert(posof0+1,"1");
		s.insert(posof0+1,"0");*/
		s.replace(posof001,3,"0");
		posof0 = s.find_first_of("0",posof001+1);
		s.replace(posof0,1,"001");
	}
	else
	{
		s[0] = '0';
		for(i=1;i<s.size();i++)
		{
			if(i%2 == 0)
				s[i] = '1';
			else
				s[i] = '0';
		}
	}
	i = s.size()-1;
	res = 0;
	while(i>=2)
	{
		node = s[i]-'0';
		if(node == 1)
			res +=  node<<i;
		i--;
	}
	return res;
}

void doRun()
{
	long res,n,t;
	int node;
	stack<int> tree;
	string s;
	scanf("%ld",&n);
	if(n == 0||n == 4)
	{
		res = n;
	}
	else
	{
		t = n;
		while(1)
		{
			node = t % 2;
			tree.push(node);
			if (t<=1)break;
			t>>=1;
		}
		if(tree.size()>30)
			return;
		while(!tree.empty())
		{
			s.insert(0,(char*)(tree.top()+'0'));
			tree.pop();
		}
		res = NextTreeIdentify(s);
	}
	printf("%ld\n",res);
}


int main()
{
	//while(1)
	doRun();
	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