您的位置:首页 > 理论基础 > 计算机网络

hihoCoder1228(2015北京网络赛B题)

2015-09-22 11:00 633 查看
题意:

给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: