算法竞赛入门经典第六章

习题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 二叉树重建

卧槽。。。。。。。。。。。。。今天写的题全部都丢了我。。。。。。。。。。。。。。。。哭了,再见吧第六章。。。。。。。。。。。。数据结构我要哭了,算法都还没开始学习,真是个废柴我本人了。。。。。。不想继续生存下去了。。。。。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值