2025年东北大学计算机考研复试机试真题
2025年东北大学计算机考研复试上机真题
历年东北大学计算机考研复试上机真题
历年东北大学计算机考研复试机试真题
更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream
N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。
出栈入栈合法性
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb
编写算法判断字符串表示的出栈入栈序列是否合法,其中A代表入栈,D代表出栈,仅有这两个字符构成。栈初始为空,例如“ADAADDDD”为非法序列,“ADAADDAD”为合法序列。
输入输出格式
输入描述:
输入一行字符串
输出描述:
如果合法输出yes,不合法输出no
输入输出样例
输入样例#:
ADAADDDD
输出样例#:
no
代码一
- #include<iostream>
- using namespace std;
- int main() {
- int sum = 0;
- string a;
- cin >> a;
- for (int i = 0; i < a.size(); i++) {
- if (a[i] == 'A') {
- sum++;
- }
- else {
- sum--;
- }
- if (sum < 0) {
- cout << "no";
- return 0;
- }
- }
- if (sum == 0) {
- cout << "yes";
- }
- else {
- cout << "no";
- }
- return 0;
- }
代码二
- #include<iostream>
- using namespace std;
- int main()
- {
- int num=0;
- string str;
- cin>>str;
- for(int i=0;i<str.size();i++)
- {
- if(str[i]=='A')
- num++;
- else
- num--;
- if(num<0)
- {
- cout<<"no";
- return 0;
- }
- }
- cout<<"yes";
- return 0;
- }
代码三
- #include <iostream>
- #include<algorithm>
- #include <cstdio>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <map>
- int a[100001];
- int b[100001];
- using namespace std;
- int main() {
- string a;
- cin >> a;
- int k = a.length();
- int flag = 0;
- for (int i = 0; i < k; i++) {
- if (a[i] == 'A')flag++;
- if (a[i] == 'D')flag--;
- if (flag < 0) {
- cout << "no";
- return 0;
- }
- }
- cout << "yes";
- return 0;
- }