Posted: Sun Feb 06, 2005 6:20 pm
OK, now I get AC. The bug is the printf("\b\n");
Code: Select all
#include <stdio.h>
#include <math.h>
#include <string.h>
#define max 500
int m[max][max];
int f=1,n;
char nn[15],s[30],inp[30];
int row,col,hr,hc;
void process(){
int i,j,k;
if(f==0){
printf("not complete\n");
return;
}
for(i=hr;i>0 && f;i--){ //check if parents exist
hc=1<<i;
for(j=hc-1;j>=0 && f;j--)
if(m[i][j]>0 && m[i-1][j/2]==0)f=0;
}
if(f==0){
printf("not complete\n");
return;
}
for(i=0;i<=hr;i++){ // print the value which is>0
k=1<<i;
for(j=0;j<=k;j++)
if(m[i][j]!=0)printf(" %d",m[i][j]);
}
printf("\n");
return;
}
void init(){
int i,j,k;
for(i=0;i<=hr;i++){
k=1<<i;
for(j=0;j<=k;j++)
m[i][j]=0;
}
}
int main(){
int i,j,k;
while(scanf("%s",inp)!=EOF){
if(strcmp(inp,"()")==0){
process();
init();
hr=0;
f=1;
}
else{
if(f==0)continue;
for(i=1,j=0;inp[i]!=',';i++) // get the value of node from input
nn[j++]=inp[i];
nn[j]=NULL;
sscanf(nn,"%d",&n);
i++;
for(i,j=0;inp[i]!=')';i++) // get the position from input
s[j++]=inp[i];
s[j]=NULL;
row=strlen(s);
if(row>hr)hr=row;
col=0;
for(i=0;s[i];i++){ //build table
if(s[i]=='L')col*=2;
else if(s[i]=='R')col=col*2+1;
}
if(m[row][col]==0) //finding diferent node in same position
m[row][col]=n;
else f=0; // if 2 node found in same position flag=0
}
}
return 0;
}
Code: Select all
(1,)
(2,L)
(3,LL)
(4,LLL)
(5,LLLL)
(6,LLLLL)
(7,LLLLLL)
(8,LLLLLLL)
(9,LLLLLLLL)
(10,LLLLLLLLL)
(11,LLLLLLLLLL)
(12,LLLLLLLLLLL)
(13,LLLLLLLLLLLL)
(14,LLLLLLLLLLLLL)
(15,LLLLLLLLLLLLLL)
(16,LLLLLLLLLLLLLLL)
(17,LLLLLLLLLLLLLLLL)
(18,LLLLLLLLLLLLLLLLL)
(19,LLLLLLLLLLLLLLLLLL)
(20,LLLLLLLLLLLLLLLLLLL)
(21,LLLLLLLLLLLLLLLLLLLL)
()
Code: Select all
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Code: Select all
so, I think the problem is in input:
Code: Select all
can anyone help me ???
thanks
Code: Select all
#include<iostream>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
struct tree
{
map<string,string> member;
};
map<string,string>::iterator pos,pos1;
string s;
int max=0,count=0;
tree level[20];
while(cin>>s)
{
string str1,str2;
if(s!="()")
{
int i=1;
while(s[i]!=',')
{
str1.push_back(s[i]);
++i;
}
++i;
while(s[i]!=')')
{
str2.push_back(s[i]);
++i;
}
if(max<str2.length())
max=str2.length();
level[str2.length()].member.insert(make_pair(str2,str1));
count++;
}
else
{
int count1=0;
for(int i=0;i<=max;++i)
{
count1=count1+level[i].member.size();
if(level[i].member.size()==0)
{
count1=-1;
break;
}
}
int i,mark=0;
if(count1!=count)
printf("not complete\n");
else
{
for(i=max;i>=2;--i)
for(pos=level[i].member.begin();pos!=level[i].member.end();++pos)
{
string test;
int y=pos->first.length()-1;
for(int k=0;k<y;++k)
test.push_back(pos->first[k]);int o=level[i-1].member.size();
level[i-1].member.insert(make_pair(test," "));
if(level[i-1].member.size()>o)
{
mark=101;
break;
}
}
if(mark==101)
printf("not complete");
else
for(i=0;i<=max;++i)
{
for(pos=level[i].member.begin();pos!=level[i].member.end();++pos)
cout<<pos->second<<" ";
level[i].member.clear();
}
cout<<"\n";
}
count=0,max=0;
}
}
}
Code: Select all
removed after a.c
Code: Select all
level[i-1].member.insert(make_pair(test," "));
Code: Select all
string space(" ");
level[i-1].member.insert(make_pair(test,space));