题一
给定一个括号字符串,请回答其中有多少对括号是匹配的,并且将未匹配的剩余字符按原顺序输出。(),[],{} 各视为1对匹配的括号; ([]) 视为2对匹配的括号 ;)(, [} 均视为不匹配的括号。
输入格式
多组数据输入
每组数据,输入一行,包含一个字符串S(1≤|S|≤105) 仅包含 ( ) [ ] { }输出格式
对于每一组数据输出两行
第一行输出一个数代表有多少对括号是匹配的
第二行输出剩余未匹配的字符串(可以是空白的)输入样例
(){} ([]){[}]
输出样例
2 2 {[}]
题解
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
//本题的核心思想是拿数组模拟队列,但在使用队列的时候要将它看作可以对栈底处理的栈(立起来)
int main()
{
string a;
while (cin >> a)
{
int numble = 0;
int cnt = a.size();
int hh = 0; //栈底
int tt = -1; //栈顶
string result = ""; //要输出的字符串
char stk[1000010] = { 0 }; //建立栈
for (int i = 0; i < cnt; i++)//开始遍历字符串
{
if (a[i] == '[' || a[i] == '(' || a[i] == '{')
{
stk[++tt] = a[i]; //进栈
}
else if (a[i] == ')'&&stk[tt] == '(') //像消消乐一样,满足栈顶和待处理字符匹配就出栈
{
tt--; //出栈
numble++;//匹配上了,结果数累加
}
else if (a[i] == ']'&&stk[tt] == '[')
{
tt--;
numble++;
}
else if (a[i] == '}'&&stk[tt] == '{')
{
tt--;
numble++;
}
else if (a[i] == '[' || a[i] == '(' || a[i] == '{')
{
result += stk[hh];
hh++;
}
else stk[++tt] = a[i];//没匹配上,就先入栈
}
while (hh <= tt)//将栈底部的元素添在result之后
{
result += stk[hh];
hh++; //栈底元素出栈,
}
cout << numble << endl;
cout << result << endl;
}
return 0;
}
题二
题目描述
假设入栈序列为1 2 3 4 ... n,则出栈序列是1到n的一个排列。 假设用P表示入栈操作,用Q表示出栈操作,则栈操作过程可以表示为一个由P和Q构成的序列。 对一个给定的出栈序列,应该如何操作才能得到呢?
输入格式
输入由若干行构成,每一行是一组由空格间隔开的整数,第一个整数是序列的长度n(n不大于1000),后面是一个1到n的排列。
输出格式
对每一行输入,计算对应的栈操作序列,并输出此操作序列,如果不能输出此序列,则在不能操作的位置输出"error"。
输入样例
4 1 2 3 4 4 4 3 2 1 4 4 2 1 3 4 3 1 2 4
输出样例
PQPQPQPQ PPPPQQQQ PPPPQ error PPPQ error
题解
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
int b[1000 + 5];
int top = 0;
memset(b, 0, sizeof(b));
stack<int> Stack;
Stack.push(top);
for (int i = 0; i < n; i++)
{
cin >> b[i];
}
for (int i = 0; i < n; i++)
{
while (Stack.top() < b[i])
{
top++;
Stack.push(top);
cout << "P";
}
if (Stack.top() == b[i])
{
cout << "Q";
Stack.pop();
}
else if (Stack.top() > b[i])
{
cout << " error";
break;
}
}
cout << endl;
}
return 0;
}
题三
题目描述
我们大多都有在银行排队的经历,唉,那坑爹的排队啊!现在就让我们来算算我们这些客户平均需要等多久吧。
每天刚开始时银行会开m个窗口来为我们total个客户办理业务,当有客户需要办理业务时,先选择可以办理业务的窗口,如果有多个窗口可以办理业务就选择空闲时间最长的窗口,如果有多个窗口空闲的时间一样长,则选择序号小的窗口办理业务。假设我们每个人来到的时间和办理业务所需要的时间(为了简化问题,采用整数表示时间)都知道了。现在请你算算我们平均需要等待多久呢?输入格式
有多组测试数据,每组数据开始有两个正整数m(<20)和total(<200),后面有total对整数,对应客户先后到来的时间以及办理业务所需的时间。
输出格式
平均等待的时间,保留两位小数。
输入样例
2 6 1 3 4 1 5 3 9 2 13 4 13 3 3 14 0 3 2 2 2 4 5 4 7 2 11 3 12 3 12 4 12 1 13 3 15 4 19 1 22 3 23 2 2 5 0 6 0 5 0 6 7 1 7 2
输出样例
0.00 0.29 1.20
数据范围与提示
提示: 题目中选择办理的窗口有三个状态,实际上从序号自小到大查找可以最早办理业务的窗口就已经满足上述三个状态了。可以使用数组来模拟列表。 总结: 实际上数组既可以模拟堆栈又可以模拟队列。
题解
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int m, total;
while (cin >> m >> total)
{
int a[20 + 5], sum = 0;
memset(a, 0, sizeof(a));
for (int i = 0; i < total; i++)
{
int get, time, flag = 0, min = 1000000;
cin >> get >> time;
for (int j = 1; j <= m; j++)
{
if (a[j] < min)
{
min = a[j];
flag = j;
}
}
if (get >= a[flag])
{
a[flag] = get + time;
}
else
{
sum += (a[flag] - get);
a[flag] += time;
}
}
printf("%.2f\n", 1.0*sum / total);
}
return 0;
}
代码解释
输入部分:首先读取机器数量m和任务总数total。每次循环处理这些任务。
初始化:创建一个数组a,用于存储每台机器的下一个可用时间,初始值为0。
任务处理:对于每个任务,读取其到达时间arrive和处理时间time。
找到当前最早可用的机器(即a[j]最小的那个),用flag标记该机器。
检查该机器是否能在任务到达时开始处理:
- 如果能,更新机器的下一个可用时间为当前到达时间加上处理时间。
- 如果不能,计算等待时间(机器下次可用时间减去到达时间),并累加到总等待时间sum,然后更新机器的可用时间。
输出结果:最后计算并打印平均等待时间,格式为两位小数。