-
hnust_xiehonghao
-
[1369] A Breaking Computer
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
-
Oh my god.My computer is break.When I writing something,the home and the end always is working.But I also write too quakily,
even I don't see the screen.OK,when I finish a work ,can you tell me the work become in end?
We modified the sample output, please check it.
- 输入
-
This have some cases.
Every case have a sentence(length< 200000 Include (A->Z a->z 0->9)).Then '[' is the home,']' is the end;
- 输出
-
Input a sentence.
Printf the finally sentence. - 样例输入
-
123[4 123[45]6 123[45]6[7]8
- 样例输出
-
4123 451236 74512368
- 提示
-
无
- 来源
-
Three God
- 操作
http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1369
题意:输入一串字符 由于电脑坏了 有可能输入的过程中 鼠标自己乱跑 可能跑到字符串的开头 和结尾 [ 表示输入的过程中跑到了开头 ] 表示跑到了末尾
比如123[45]6[7]8 先输入123 此时 [ 跑到了开头 输入45 变成了 45123 遇到 ] 跑到最后 输入6 变成451236
之后遇到 [ 跑到开头 输入7变成了 7451236 之后遇到 ] 跑到最后 输入8 变成了 答案74512368
思路 双向队列
#include<stdio.h> #include<deque> #include<vector> using namespace std; char s[200005]; int main() { deque<char>dq(20); deque<char>::iterator it; vector<char>a; vector<char>::iterator ii; int i,flag=0,last; while(scanf("%s",s)!=EOF) { while(!dq.empty()) dq.pop_front(); a.clear(); flag=0;last=0; for(i=0;s[i]!='\0';i++) { if(s[i]!='['&&s[i]!=']') { if(flag) { a.push_back(s[i]); } else dq.push_back(s[i]); continue; } if(s[i]=='[') { flag=1; if(a.empty()) continue; ii=a.end(); ii--; for(;ii>=a.begin();ii--) dq.push_front(*ii); a.clear(); } else if(s[i]==']') { flag=0; if(a.empty()) continue; ii=a.end(); ii--; for(;ii>=a.begin();ii--) dq.push_front(*ii); a.clear(); } } if(!a.empty()) { ii=a.end(); ii--; for(;ii>=a.begin();ii--) dq.push_front(*ii); a.clear(); } it=dq.begin(); for(it;it!=dq.end();it++) printf("%c",*it); printf("\n"); } return 0; }
反思: 本题 我一开始用1个小时的时间 才拍出一个通过样例 的程序 居然发现自己理解错题意了 每次都是这样 自己的这个缺点很严重 以后千万要注意 -
另外 如果一个程序修改了很长时间 还很复杂 而且代码不长 一定要重新拍一个 这样思路更加清晰
宁波工程学院 [1369] A Breaking Computer deque的应用
最新推荐文章于 2024-03-29 13:08:21 发布