您的位置:首页 > 大数据 > 人工智能

hdu 1022 Train Problem I (栈的应用)

2014-05-05 18:02 459 查看
[align=left]Sample Input[/align]

3 123 321
3 123 312


[align=left]Sample Output[/align]
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

考察栈的应用,给定序列str1和str2,问你能否在str1的基础上只进行入栈 出栈得到str2
用stl水就可以了
但是我水的很艰难啊


各种WA
直接上代码吧:
[code]#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#define MAXN 10010
#define ll long long
using namespace std;
vector<string> ans;
int main(void) {
int n;
string str1, str2;
stack<char> st;
while(scanf("%d", &n) != EOF) {
ans.clear();
while(!st.empty())//用栈的时候千万记得清空栈
st.pop();
cin >> str1 >> str2;
int len = str1.size();
int i = 0;
int j = 0;
while(i<len) {
st.push(str1[i++]);
ans.push_back("in");
while(!st.empty() && st.top() == str2[j]) {//每次要一直退栈到不能退为止
st.pop();
ans.push_back("out");
++j;
}
}

while(j < len) {
if(st.top() == str2[j]) {
st.pop();
ans.push_back("out");
}
++j;
}
if(st.empty()) {
cout << "Yes." << endl;
for(int i=0; i<ans.size(); ++i) {
cout << ans[i] << endl;
}
}
else cout << "No." << endl;
cout << "FINISH" << endl;
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: