CSU---D: Simple Line Editor

àì夳堔傛蜴生んèń 2022-06-16 14:50 137阅读 0赞

Submit Page Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 64 Solved: 25


Description

Early computer used line editor, which allowed text to be created and changed only within one line at a time. However, in line editor programs, typing, editing, and document display do not occur simultaneously (unlike the modern text editor like Microsoft Word). Typically, typing does not enter text directly into the document. Instead, users modify the document text by entering simple commands on a text-only terminal.

Here is an example of a simple line editor which can only process English. In addition, it has two commands. ‘@’ and ‘#’. ‘#’ means to cancel the previous letter, and ‘@’ is a command which invalidates all the letters typed before. That is to say, if you want type “aa”, but have mistakenly entered “ab”, then you should enter ‘#a’ or ‘@aa’ to correct it. Note that if there is no letter in the current document, ‘@’ or ‘#’ command will do nothing.

Input

The first line contains an integer T, which is the number of test cases. Each test case is a typing sequence of a line editor, which contains only lower case letters, ‘@’ and ‘#’.

there are no more than 1000 letters for each test case.

Output

For each test case, print one line which represents the final document of the user. There would be no empty line in the test data.

Sample Input2

2

ab#a

ab@aa

Sample Output

aa

aa

题意:有两种字符#和@,#表示删除前一个字符,@表示删除前面所有的字符

给出n个字符串,输出处理后的字符串

赋值的时候y没有在for循环里赋值,结果错了好几次

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<string>
  5. #include<algorithm>
  6. #include<iostream>
  7. using namespace std;
  8. int main()
  9. {
  10. int y,n;
  11. string str;
  12. scanf("%d",&n);
  13. for(int i=0; i<n; i++)
  14. {
  15. cin>>str;
  16. y=-1;
  17. for(int j=str.length()-1; j>=0; j--)//找到@字符的位置
  18. {
  19. if(str[j]=='@')
  20. {
  21. y=j;
  22. break;
  23. }
  24. }
  25. str.erase(0,y+1);//删除@前面所有字符
  26. //cout<<str<<endl;
  27. for(int j=1; j<str.length(); j++)
  28. {
  29. if(str[j]=='#')//是#就删除 它 和 它前面字符(两个都要删除)
  30. {
  31. str.erase(j-1,2);
  32. j=j-2;//删除之后字符串长度会改变,记得减去2
  33. if(j<0)//如果变成了负数,从0开始即可,不然会指向空地址,报错
  34. j=0;
  35. }
  36. //cout<<j<<" "<<str.length()<<endl;
  37. }
  38. while(str[0]=='#')
  39. str.erase(0,1);
  40. cout<<str<<endl;
  41. }
  42. return 0;
  43. }

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读