课件(下载)
#include <iostream>
#include <string>
using namespace std;
#define N 27
char pre[N];
char in[N];
void PostOrder(int preStart,int preEnd,int inStart,int inEnd)
{
if( preStart <= preEnd )
{
char root = pre[preStart];
int leftLen;
for( int i=inStart;i<=inEnd;i++ )
{
if( root == in[i] )
{
leftLen = i - inStart;
break;
}
}
PostOrder(
preStart+1,
preStart + leftLen,
inStart,
inStart+leftLen-1
);
PostOrder(
preStart + leftLen+1,
preEnd,
inStart + leftLen + 1,
inEnd
);
cout<<root;
}
}
int main()
{
while(cin>>pre>>in)
{
PostOrder(0,strlen(pre)-1,0,strlen(in)-1);
cout<<endl;
}
return 1;
}
#include <map>
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
class MySort
{
public:
bool operator ()(const string &_A,const string &_B) const
{
if(_A.length() < _B.length() )
return true;
if(_A.length()==_B.length() && _A < _B )
return true;
return false;
}
};
map<string,int,MySort> data;
bool InsertNode(string str)
{
str = str.substr(1,str.length()-2);
int pos = str.find(',');
string key = str.substr(pos+1);
int value = atoi( str.substr(0,pos).c_str() );
if( data.find(key)!=data.end() )
return false;
else
{
pair<map<string,int,MySort>::iterator,bool> insertPair;
insertPair = data.insert( map<string,int,MySort>::value_type(key,value) );
return insertPair.second;
//data[key] = value;
//return true;
}
}
bool HasRoot()
{
//cout<<"asdf';
for(map<string,int,MySort>::iterator i=data.begin();i!=data.end();i++)
{
string str = i->first;
//cout << ' ' << str.substr(0,str.length()-1) << ' ';
if( str.length() == 0 )
continue;
if( data.find(str.substr(0,str.length()-1)) == data.end() )
return false;
}
return true;
}
int main()
{
string str;
bool complete = true;
while( cin>>str )
{
if(str == "()")
{
//一个测试结束,给出结论
if( complete && HasRoot() )
{
map<string,int,MySort>::iterator i;
for( i=data.begin();i!=data.end();i++ )
{
if( i!=data.begin() )
cout << ' ';
cout << (*i).second ;
}
}
else
{
cout << "not complete";
}
cout << endl;
//新测试开始
//初始化
data.clear();
complete = true;
}
else
{
//加入map
if( !InsertNode(str) )
complete = false;
}
}
return 1;
}