CSU->1019: Simple Line Editor

7 篇文章 0 订阅
7 篇文章 0 订阅

1019: Simple Line Editor

            Time Limit: 1 Sec     Memory Limit: 128 Mb        

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 Input

2
ab#a
ab@aa

Sample Output

aa
aa

Hint

Source

中南大学第四届大学生程序设计竞赛

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1019

题解:本题就是要求对@之前的所有字符都删掉,#前的一个字符被#后的一个字符代替,原字符串需要减2个长度。这题可以用栈,字符串,vector容器来解,具体看下面代码。

1.在原字符串上删除修改:

#include<string>
#include<iostream>
using namespace std;
int main()
{
    int y,n;
    string str;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        cin>>str;
        y=-1;
        for(int j=str.length()-1; j>=0; j--)        //找最后一个@字符的位置
        {
            if(str[j]=='@')
            {
                y=j;
                break;
            }
        }
        str.erase(0,y+1);       //删除@前面所有字符
        for(int j=1; j<str.length(); j++)
        {
            if(str[j]=='#')     //是#就删除它和它前面字符两个都要删除)
            {
                str.erase(j-1,2);
                j=j-2;      //删除之后字符串长度会改变,记得减去2
                if(j<0)     //如果变成了负数,从0开始即可,不然会指向空地址,报错
                    j=0;    
            }
        }
        while(str[0]=='#')
            str.erase(0,1);
        cout<<str<<endl;
    }
    return 0;
}

2.在另一空字符串上拼接:

#include <iostream>
#include <string.h>
using namespace std;
int N;
string s;
string tmp;
int i;
int ti;
int main()
{
    scanf("%d",&N);
    while(N--)
    {
        cin>>s;
        tmp.clear();
        for(int j = s.length() - 1 ; j >= 0 ; j--)
        {
            if(s[j] == '@')
            s = s.substr(j+1,s.length() - j);
        }
        for(i = 0 ; i < s.length() ; i++)
        {
            if(s[i] == '#')
            {
                if(tmp.empty())
                    continue;
                else
                {
                    tmp = tmp.substr(0,tmp.length() - 1);
                }
            }
            else
            {
                tmp += s[i];
            }
        }
        cout<<tmp<<endl;
    }
    return 0;
}

3.使用栈:

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main() {
    int n, l;
    char c[1010], ans[1010];
    cin>>n;
    getchar();
    while(n--) {
        l = 0;
        stack<char>s;
        gets(c);
        for(int i = 0; i < strlen(c); i++) {
            if(c[i]=='#') {
                if(!s.empty())
                    s.pop();
            } else if(c[i]=='@') {
                while(!s.empty()) {
                    s.pop();
                }
            } else {
                s.push(c[i]);
            }
        }
        while(!s.empty()) {
            ans[l++] = s.top();
            s.pop();
        }
        for(int i = l-1; i >= 0; i--) {
            cout<<ans[i];
        }
        cout<<endl;
    }
    return 0;
}

4.使用vector容器:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<char> v;
string s;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        v.clear();
        cin>>s;
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='@')
            {
                if(!v.empty())
                    v.clear();
            }
            else if(s[i]=='#')
            {
                if(!v.empty()) v.pop_back();
            }
            else v.push_back(s[i]);
        }
        vector<char>::iterator it;
        for(it=v.begin();it!=v.end();it++)
        {
            cout<<*it;
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值