hihoCoder1228(2015北京网络赛B题)
2015-09-22 11:00
633 查看
题意:
给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令:
L:光标左移;
R:光标右移;
S:在insert模式和另一个输入模式中切换;
D:删除光标后面的一个字符;
B:删除光标前面的一个字符;
C:复制;
V:粘贴;
思路:
模拟。结果这题我们WA了全场。赛后回去检查,发现了我愚蠢的地方。。。就是D操作时,如果在复制状态下,我把删除的起点选错了。。。教训啊!!!
代码:
给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令:
L:光标左移;
R:光标右移;
S:在insert模式和另一个输入模式中切换;
D:删除光标后面的一个字符;
B:删除光标前面的一个字符;
C:复制;
V:粘贴;
思路:
模拟。结果这题我们WA了全场。赛后回去检查,发现了我愚蠢的地方。。。就是D操作时,如果在复制状态下,我把删除的起点选错了。。。教训啊!!!
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cstdlib> #include<cmath> #include<map> #include<vector> #include<stack> #include<queue> #include<string> #include<climits> using namespace std; string ans,cc,in; int main() { int t; cin>>t; while(t--) { int lim; cin>>lim>>in; int len=in.length(); int insertmode=1; int copy=-1; int pos=0; ans=""; cc=""; int real1=-1,real2=-1; for(int i=0;i<len;i++) { if(in[i]>='a'&&in[i]<='z') { if(insertmode==1) { if(ans.length()>=lim) continue; ans.insert(pos,1,in[i]); pos++; } else { if(pos==ans.length()) { if(ans.length() == lim) continue ; ans+=in[i]; pos++; }else { ans[pos] = in[i]; pos++; } } copy=-1; } else { int tmplen=ans.length(); if(in[i]=='L') { if(pos>0) pos--; } else if(in[i]=='R') { // cout<<"tt="<<tmplen<<endl; if(pos<(tmplen)) pos++; } else if(in[i]=='S') { if(insertmode==1) insertmode=0; else insertmode=1; copy=-1; } else if(in[i]=='D') { if(copy==1) { int tt=real1; real1=min(tt,pos); real2=max(tt,pos); //cout<<real1<<" "<<real2<<endl; ans.erase(real1,real2-real1); //ans.erase(pos,real2-real1);这里写错了 } else if(pos<tmplen) ans.erase(pos,1); copy=-1; } else if(in[i]=='B') { // cout<<"==="<<ans<<endl; //cout<<"pos="<<pos<<endl; if(pos>0) { ans.erase(pos-1,1); pos--; } copy=-1; // cout<<"---"<<ans<<endl; } else if(in[i]=='C') { if(copy==-1) { real1=real2=pos; copy=1; } else { cc=""; int tt=real1; real1=min(tt,pos); real2=max(tt,pos); // cout<<real1<<" "<<real2<<endl; // cout<<pos1<<" "<<pos1<<endl; // puts("----"); cc=ans.substr(real1,real2-real1); // puts("----"); // cout<<"cc="<<cc<<endl; copy=-1; } } else if(in[i]=='V') { if(insertmode==1) { //cout<<cc<<" "<<pos<<" "<<ans<<endl; if(cc.length()==0) continue; if(cc.length()+ans.length()>lim) continue; ans.insert(pos,cc); pos+=cc.length(); } else { //cout<<cc<<" "<<pos<<" "<<ans<<endl; if(pos+cc.length()>lim) continue; ans.replace(pos,cc.length(),cc); pos+=cc.length(); } copy=-1; } } //cout<<cc<<endl; // cout<<"----"<<ans<<" "<<pos<<" "<<pos1<<" "<<pos2<<endl; } if(ans.length()==0) cout<<"NOTHING"<<endl; else cout<<ans<<endl; } return 0; }
相关文章推荐
- Java____HTTP协议Cookies详解
- TCP IP HTTP SOAP WEB SERVICE
- iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)
- 函数调用分析 http://www.cnblogs.com/bangerlee/archive/2012/05/22/2508772.html
- [转载]MAC、IP头、TCP头、UDP头帧格式、详解
- 【转】 httpclient 模拟浏览器动作需注意的cookie和HTTP头等信息
- hihoCoder 北京网络赛 #1234 : Fractal
- 用gdb 查看,执行汇编代码 http://blog.csdn.net/hejinjing_tom_com/article/details/26704487
- Nginx 的 TCP 负载均衡介绍
- 一只简单的网络爬虫(基于linux C/C++)————开篇
- Android中getSystemService介绍,获取系统的运行状态,判断当前网络状态
- Curl POST to HTTPS url gives SSLRead() error:curl: (56) SSLRead() return error -9806
- iOS iOS9 HTTP请求失败
- 技术杂谈(1)——Vmware网络连接模式简介
- socket通讯原理以及tcp、ip三次握手机制分析
- 进程0 进程1 http://blog.chinaunix.net/uid-26874138-id-3183711.html
- 使用AsyncTask异步更新UI界面(加载网络图片)
- 终于明白#!bin/sh是什么意思了 (2009-10-06 21:58:47) http://blog.sina.com.cn/s/blog_4d32d0b40100fngc.html
- 计算机网络面试常考
- win7 VMware CentOS桥接(bridge)模式网络配置