牛客练习赛31 D 神器大师泰兹瑞与威穆
https://ac.nowcoder.com/acm/contest/218/D
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
「只要我拉动绳线,你就得随之起舞。」 ——泰兹瑞
泰兹瑞来到卡拉德许之后,由于他精湛的神器制造技术,可谓是过的如鱼得水。这次,他为自己打造了一个编辑器,称为威穆(Veim)。操作威穆时,有两种模式,具体操作如下。
在 Normal Mode 下
- 按下 i :进入 Insert Mode 。
- 按下 f :紧接着一个小写字母
char,若当前光标后(右)方有至少一个 char ,将光标移动到其所在位置,否则不移动。
- 按下 x :删除当前光标所在位的字符,后面的字符均会前移一格。
- 按下 h :将光标向左(前)移动一格,若无法移动就不移动。
- 按下 l :将光标向右(后)移动一格,若无法移动就不移动。
- 若按下了其他字符:无任何效果。
在 Insert Mode 下
- 按下非 e 小写字母 char :在光标当前位置前插入这个字母
char。
- 按下 e :退出 Insert Mode(进入 Normal Mode)。
(具体请见样例)
现在泰兹瑞的威穆中已经写入了一个字符串 s 。接下去泰兹瑞进行了一波操作(按下了若干按键),他的按键序列为 t 。现给出 s 和 t ,求这波操作之后威穆内留下的字符串。
输入描述:
两行,第一行字符串 s ,第二行字符串 t 。
输出描述:
一行,威穆里最后留下的字符串。
示例1
输入
applese
xfllhlia
输出
pplaese
说明
- 初始时,字符串为 ,威穆处于 Normal Mode 。下划线表示光标所在位置。
- 第一步操作为 x ,删除当前光标所在位的字符,并且光标后移一格。字符串变为 。威穆仍处于 Normal Mode。
- 下一步操作为 f ,之后跟有一个字符 `l` 。光标后存在字符
`l` ,故移动到该位置。字符串变为 。威穆仍处于 Normal Mode。
- 下一步操作为 l ,光标后移一格。字符串变为 。威穆仍处于 Normal Mode。
- 下一步操作为 h ,光标前移一格。字符串变为 。威穆仍处于 Normal Mode。
- 下一步操作为 l ,光标后移一格。字符串变为 。威穆仍处于 Normal Mode。
- 下一步操作为 i ,威穆进入 Insert Mode。字符串仍为
。
- 下一步操作为 a ,而非 e ,故插入字符 a 。字符串变为 。
示例2
输入
pppp
iaefpfpia
输出
appapp
备注:
1 ≤ |s|, |t| ≤ 105
s, t 均由小写拉丁字母组成。
分析
看完题目之后,意识到除了模拟别无他法,于是迅速暴力手写完成,其中normal mode中的f比较迷,,看了半天才看懂,然后提交TLE QAQ
TLE代码:
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <string> 7 #include <time.h> 8 #include <queue> 9 #include <string.h> 10 #define sf scanf 11 #define pf printf 12 #define lf double 13 #define ll long long 14 #define p123 printf("123 "); 15 #define pn printf(" "); 16 #define pk printf(" "); 17 #define p(n) printf("%d",n); 18 #define pln(n) printf("%d ",n); 19 #define s(n) scanf("%d",&n); 20 #define ss(n) scanf("%s",n); 21 #define ps(n) printf("%s",n); 22 #define sld(n) scanf("%lld",&n); 23 #define pld(n) printf("%lld",n); 24 #define slf(n) scanf("%lf",&n); 25 #define plf(n) printf("%lf",n); 26 #define sc(n) scanf("%c",&n); 27 #define pc(n) printf("%c",n); 28 #define gc getchar(); 29 #define re(n,a) memset(n,a,sizeof(n)); 30 #define len(a) strlen(a) 31 #define LL long long 32 #define eps 1e-6 33 using namespace std; 34 char s[100500],t[100500]; 35 int main() { 36 ss(s); 37 ss(t); 38 int sta = 0;//0是normal ,1是insert 39 int lens = len(s); 40 int lent = len(t); 41 int x = 0; 42 for(int i = 0; i < lent; i ++) { 43 if(sta == 0) { 44 if(t[i] == 'i') { 45 sta = 1; 46 } else if(t[i] == 'f') { 47 char c = t[i+1]; 48 for(int i = x+1; i < lens; i ++) { 49 if(s[i] == c) { 50 x = i; 51 break; 52 } 53 } 54 55 i ++; 56 } else if(t[i] == 'x') { 57 for(int i = x; i < lens; i ++) { 58 s[i] = s[i+1]; 59 } 60 lens --; 61 } else if(t[i] == 'h') { 62 if(x != 0) { 63 x --; 64 } 65 } else if(t[i] == 'l') { 66 if(x != lens-1) { 67 x++; 68 } 69 } 70 } else { 71 if(t[i] == 'e') { 72 sta = 0; 73 } else { 74 for(int i = lens+1; i >= x+1; i --) { 75 s[i] = s[i-1]; 76 } 77 s[x] = t[i]; 78 x ++; 79 } 80 } 81 //ps(s) pn 82 } 83 ps(s) pn 84 }
打过一遍之后对题目有很深的理解了,意识到用链表做比较轻松,因为中间存在一个光标,直接调整光标插入删除速度很快,
But, But! But!! But!!!
不会list啊。。。。。。。。
绝望,,,,,自闭了。。。。。
现场学list:
https://www.cnblogs.com/lalalabi/p/5060210.html
https://www.cnblogs.com/loleina/p/5179677.html
https://blog.csdn.net/zhouzhenhe2008/article/details/77428743/
https://blog.csdn.net/amoscykl/article/details/80934961
然后掌握了基本用法之后在题目里还有坑,
需要用到迭代器遍历链表,在检查的时候因为这个改了好多遍,,,还WA了一发,所以标红:如果要遍历检查一定要新建一个迭代器!!!
然后顺利AC
AC代码:
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <string> 7 #include <time.h> 8 #include <queue> 9 #include <string.h> 10 #include <list> 11 #define sf scanf 12 #define pf printf 13 #define lf double 14 #define ll long long 15 #define p123 printf("123 "); 16 #define pn printf(" "); 17 #define pk printf(" "); 18 #define p(n) printf("%d",n); 19 #define pln(n) printf("%d ",n); 20 #define s(n) scanf("%d",&n); 21 #define ss(n) scanf("%s",n); 22 #define ps(n) printf("%s",n); 23 #define sld(n) scanf("%lld",&n); 24 #define pld(n) printf("%lld",n); 25 #define slf(n) scanf("%lf",&n); 26 #define plf(n) printf("%lf",n); 27 #define sc(n) scanf("%c",&n); 28 #define pc(n) printf("%c",n); 29 #define gc getchar(); 30 #define re(n,a) memset(n,a,sizeof(n)); 31 #define len(a) strlen(a) 32 #define LL long long 33 #define eps 1e-6 34 using namespace std; 35 list<char> l; 36 char s[100500],t[100500]; 37 int main(){ 38 //ss(s); 39 //ss(t); 40 cin >> s >> t; 41 int sta = 0;//0是normal ,1是insert 42 int lens = len(s); 43 int lent = len(t); 44 list<char>::iterator x; 45 //int x = 0; 46 x = l.begin(); 47 for(int i = 0; i < lens; i ++){ 48 l.insert(x,s[i]); 49 } 50 51 /*for(x = l.begin();x!=l.end();++x){ 52 cout<<*x; 53 }*/ 54 x = l.begin(); 55 for(int i = 0; i < lent; i ++){ 56 if(sta == 0){ 57 if(t[i] == 'i'){ 58 sta = 1; 59 }else if(t[i] == 'f'){ 60 char c = t[i+1]; 61 list<char>::iterator y; 62 x ++; 63 y = x; 64 x --; 65 for(;y!=l.end();++y){ 66 if(*y == c){ 67 x = y; 68 break; 69 } 70 } 71 i ++; 72 }else if(t[i] == 'x'){ 73 74 list<char>::iterator xx; 75 xx = x; 76 x++; 77 l.erase(xx); 78 79 //x = l.begin(); 80 }else if(t[i] == 'h'){ 81 if(x != l.begin()){ 82 x --; 83 } 84 }else if(t[i] == 'l'){ 85 if(x != l.end()){ 86 x++; 87 } 88 } 89 }else{ 90 if(t[i] == 'e'){ 91 sta = 0; 92 }else{ 93 l.insert(x,t[i]); 94 } 95 } 96 /*list<char>::iterator z; 97 for(z = l.begin();z!=l.end();++z){ 98 cout<<*z;p123 99 } 100 cout << endl;*/ 101 //ps(s) pn 102 } 103 //ps(s) pn 104 list<char>::iterator z; 105 for(z = l.begin();z!=l.end();++z){ 106 cout<<*z; 107 } 108 cout << endl; 109 }