习题6-1 平衡的括号
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*
()[]([][]())
*/
int main(int argc, char *argv[]) {
stack<char> p;
char s[1000];
scanf("%s",s);
cout<<s<<endl;
int len=strlen(s);
int i;
for(i=0;i<len;i++){
if(s[i]=='('||s[i]=='['){
p.push(s[i]);
cout<<s[i]<<endl;
}
else if(s[i]==')'||s[i]==']'){
if(p.empty()){
break;
//cout<<"NO"<<endl;//buhefa;
//return 0;
}
char pp=p.top();p.pop();
//if(pp!=s[i]-1||pp!=s[i]-2){
if ( (s[i] == ')' && pp == '(') || (s[i] == ']' && pp == '[') ){
continue;
}
else{
break;
//cout<<"NO"<<endl;//buhefa;
//return 0;
}
}
}
if(i>=len&&p.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
习题6-2 S树
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*
3
x1 x2 x3
00000111
4
000
010
111
110
*/
int main(int argc, char *argv[]) {
int n;
int count=1;
while(scanf("%d",&n)==1&&n){
string s,b;
char a[1000],c;
getchar();
getline(cin, s);
s.clear();
//cout<<s<<endl;
//int num=1<<n;
//cout<<num<<endl;
for(int i=0;i<(1<<n);i++){
c=getchar();
a[i]=c;
}
int m;
scanf("%d",&m);
while(m--){
int sum=0;
cin>>b;
//cout<<b<<endl;
int len=b.size();
//cout<<len<<endl;
//cout<<b[1]-'0'<<endl;
for(int i=0;i<len;i++){
sum=(sum<<1)+(b[i]-'0');
//cout<<sum<<endl;
}
s.push_back(a[sum]);
}
printf("S-Tree #%d:\n", count++);
cout<<s<<endl<<endl;
}
return 0;
}
习题6-3 二叉树重建
卧槽。。。。。。。。。。。。。今天写的题全部都丢了我。。。。。。。。。。。。。。。。哭了,再见吧第六章。。。。。。。。。。。。数据结构我要哭了,算法都还没开始学习,真是个废柴我本人了。。。。。。不想继续生存下去了。。。。。。。。。。。