2015北京网络赛B题 Mission Impossible 6
借用大牛的一张图片:模拟
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<memory.h> 7 #include<cstdlib> 8 #include<vector> 9 using namespace std; 10 const int MAXN=1000; 11 const double eps=1e-8; 12 const int inf=0x3f3f3f3f; 13 14 string s,ss,cs; 15 vector<int>pos; 16 int mode; 17 int now,m; 18 void L() 19 { 20 if(now>0) 21 now--; 22 } 23 24 void R() 25 { 26 if(now<ss.length()) 27 now++; 28 } 29 30 void S() 31 { 32 mode=!mode; 33 pos.clear(); 34 } 35 void C() 36 { 37 pos.push_back(now); 38 if(pos.size()==2) 39 { 40 sort(pos.begin(),pos.end()); 41 cs=ss.substr(pos[0],pos[1]-pos[0]); 42 pos.clear(); 43 } 44 } 45 void D() 46 { 47 if(pos.size()) 48 { 49 pos.push_back(now); 50 sort(pos.begin(),pos.end()); 51 ss.erase(ss.begin()+pos[0],ss.begin()+pos[1]); 52 now=pos[0]; 53 pos.clear(); 54 } 55 else if(now<ss.length()) 56 { 57 ss.erase(ss.begin()+now,ss.begin()+now+1); 58 } 59 } 60 61 void B() 62 { 63 if(now>0) 64 { 65 ss.erase(ss.begin()+now-1,ss.begin()+now); 66 now--; 67 } 68 pos.clear(); 69 } 70 71 void V() 72 { 73 if(cs.length()) 74 { 75 if(!mode) 76 { 77 if(ss.length()+cs.length()<=m) 78 { 79 int len=cs.length(); 80 for(int i=0; i<len; i++,now++) 81 { 82 ss.insert(ss.begin()+now,cs[i]); 83 } 84 } 85 } 86 else 87 { 88 if(now+cs.length()+1<=m) 89 { 90 int len1=cs.length(); 91 int len2=ss.length(); 92 for(int i=0; i<len1; i++,now++) 93 { 94 if(now>=len2) 95 ss.insert(ss.begin()+now,cs[i]); 96 else 97 ss[now]=cs[i]; 98 } 99 } 100 } 101 } 102 pos.clear(); 103 } 104 105 int main() 106 { 107 int T; 108 scanf("%d",&T); 109 while(T--) 110 { 111 cin>>m>>s; 112 int len=s.length(); 113 mode=0; 114 now=0; 115 ss.clear(); 116 cs.clear(); 117 pos.clear(); 118 for(int i=0; i<len&&m; i++) 119 { 120 if(s[i]=='L') 121 L(); 122 else if(s[i]=='R') 123 R(); 124 else if(s[i]=='C') 125 C(); 126 else if(s[i]=='V') 127 V(); 128 else if(s[i]=='D') 129 D(); 130 else if(s[i]=='B') 131 B(); 132 else if(s[i]=='S') 133 S(); 134 else 135 { 136 if(mode&&now<ss.length()) 137 { 138 ss[now++]=s[i]; 139 } 140 else if(ss.length()<m) 141 { 142 ss.insert(ss.begin()+now,s[i]); 143 now++; 144 } 145 pos.clear(); 146 } 147 } 148 if(ss.size()) 149 { 150 cout<<ss<<endl; 151 } 152 else 153 cout<<"NOTHING"<<endl; 154 } 155 return 0; 156 }