杭电1022 train problem
2016-05-15 10:17
387 查看
主要是栈的应用
最后一定注意栈要清空。
int main()
{
}
最后一定注意栈要清空。
#include<string> #include<stack> #include<iostream>
int main()
{
int n; string inOrder, outOrder; stack<char> train; char c; while (cin >> n >> inOrder >> outOrder) { int j = 0; int k = 0; int in_or_out[18]; for (int i = 0; i < n; i++) { while (j < n && (train.empty() || train.top() != outOrder[i])) //在inOrder未遍历完时进行入栈操作,在栈为空时一定会先入栈或者栈不为空但是栈顶字符不等于当前outOrder元素也会一直入栈直到栈顶字符和当前字符相同。 { train.push(inOrder[j++]); in_or_out[k++] = 1; } if (train.top() != outOrder[i]) break;//经过上述循环,此时的栈顶有两种可能,等于当前字符或者不等于当前字符,若不等,那么就是说这个字符在进行上述循环前栈内(非栈顶)也就不可能下一个出栈。 else//否则就是出栈 { train.pop(); in_or_out[k++] = 0; } } if (!train.empty()) cout << "No." << endl; else { cout << "Yes." << endl; for (int i = 0; i < k; i++) { if (in_or_out[i]) cout << "in" << endl; else cout << "out" << endl; } } cout << "FINISH" << endl; while (!train.empty())//最后一定要让栈空,因为循环中使用的是一个栈,若不清空,则在进行若干操作后,栈中保存有上一次的字符,所以进栈循环中的判断条件train.empty()没用了。 train.pop(); } return 0;
}
相关文章推荐
- Writing a ServiceMain Function(使用RegisterServiceCtrlHandler函数)
- 具有超强阅读理解能力的人工智能(Maluuba机器人)
- http://blog.csdn.net/jhg1204/article/details/45013987
- STL中的Pair方法详解
- HDU 2115 I Love This Game(结构体排序 or pair)
- HDU 5228-ZCC loves straight flush
- Domain Transform for Edge-Aware Image and Video Processing - 论文阅读
- At least one JAR was scanned for TLDs yet contained no TLDs 解决办法
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- hdoj-1789-Doing Homework again
- 11. Container With Most Water
- hdu_1532_Drainage Ditches
- uva10651 Pebble Solitaire(记忆化搜索)
- 1106. Lowest Price in Supply Chain (25)【树+深搜】——PAT (Advanced Level) Practise
- 1090. Highest Price in Supply Chain (25)【树】——PAT (Advanced Level) Practise
- 无法解析的外部符号WinMain@16
- 谷歌黑科技:联机版人工智能系统
- 172. Factorial Trailing Zeroes
- Contains Duplicate II
- sleep方法和wait方法的区别