【线性数据结构】03_栈

1、栈的基本使用

操作受限的线性表,元素只能从栈顶出入,使用时加头文件 #include <stack>。

stack<typename>  myStack定义

myStack.push()

压栈
myStack.pop()弹栈
myStack.empty()判断是否为空,返回bool
myStack.top()获取栈顶元素
myStack.size()获取栈大小

依次压入0-9再弹出栈:

#include <cstdio>
#include <stack>

using namespace std;

int main(){
	stack<int> myStack;
	printf("size of myStack is %d\n",myStack.size());
	for (int i=0;i<10;i++){
		myStack.push(i);
	}
	printf("size of myStack is %d\n",myStack.size());
	while(!myStack.empty()) {
		printf("cur top is %d\n",myStack.top());
		myStack.pop();
	}
	printf("My stack is empty now.\n");
}

2、栈的例题

牛客网 KY109 Zero-complexity Transposition

描述

You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.

输入描述:

For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).

输出描述:

For each case, on the first line of the output file print the sequence in the reverse order.

示例1

输入:

5
-3 4 6 -8 9

输出:

9 -8 6 4 -3

【思路】☆

估计题目数据范围:参考——2^{10}=1024≈10 ^{3}

int4B=32bit-2^{31}~0~2^{31}-1-2x10 ^{9}~2x10 ^{9}
unsigned int4B=32bit0~2^{32}-10~4x10 ^{9}
long long8B=64bit-2^{63}~0~2^{63}-1-8x10^{18}~8x10^{18}
unsigned long long8B=64bit0~2^{64}-10~16x10^{18}

 若是题目数据范围超过 16x10^{18},则将其存入字符串,自定义函数进行处理。

#include <cstdio>
#include <stack>

using namespace std;

int main(){
	stack<long long> myStack;
	long long num;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&num);
		myStack.push(num);
	}
	while(!myStack.empty()){
		printf("%lld ",myStack.top());
		myStack.pop();
	}
}

POJ括号匹配

描述

在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$“标注,不能匹配的右括号用”?"标注.

输入

输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100
注意:cin.getline(str,100)最多只能输入99个字符!

输出

对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"" , " ? " 和 空 格 组 成 , " ","?"和空格组成,"","?"和空格组成,"“和”?"表示与之对应的左括号和右括号不能匹配。

样例输入

((ABCD(x)
)(rttyy())sss)(

样例输出

((ABCD(x)
$$
)(rttyy())sss)(
? ?$

【思路】

1、使用栈的结构,左括号进栈,右括号出栈

左括号:遇到一个左括号先设置匹配失败,后面有右括号匹配上再设置匹配成功(需要能够记录位置信息)

右括号:栈为空时出栈则非法;正常匹配时,一个右括号自己合法,也可以“解救”一个栈顶左括号合法

2、读取字符串的操作  char buf[1000]

读取单词        scanf("%d",buf)  ——>Ctrl+D: EOF

读取一行        fgets(buf, 1000, stdin) ——>Ctrl+D:NULL

                      额外读取一个换行,去掉换行:strin str = buf;  str.pop_back();

#include <cstdio>
#include <stack>
#include <string>

using namespace std;

int main(){
	char buf[200];
	while(fgets(buf,200,stdin)!=NULL){//fegts配合while实现不确定数量的多行读取 
		string str = buf;
		str.pop_back();//去掉读取内容最后额外的换行
		
		stack<unsigned int> index; //储存左括号下标
		string res;//保存输出结果
		
		for(int i=0;i<str.size();i++){
			if(str[i]=='('){
				index.push(i);//左括号下标入栈
				res.push_back('$');//初始认为左括号非法 
			}
			else if(str[i]==')'){
				if(index.empty()){//栈空,此右括号匹配失败 
					res.push_back('?'); 
				}
				else{//匹配成功,
					res.push_back(' '); //自己合法了 
					res[index.top] = ' ';//与它配对的左括号置为合法
					index.pop(); 
				}
			}
			else{
				res.push_back(' '); 
			}
		}
	}
	printf("%s\n%s\n",str.c_str(),res.c_str()); //将C++风格转为C风格才能输出 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值