COJ 1064行编辑距离:STL栈的简单应用

来源:互联网 发布:安卓沙箱软件 编辑:程序博客网 时间:2024/06/11 13:00

【数据结构】行编辑程序

Time Limit: 1000 ms     Memory Limit: 65536 KB
Total Submit: 22     Accepted: 14

Description
对若干行字符进行编辑处理,并实现以下功能:
当出现‘#’时,当作退格符处理(即删除前一个字符,#不保留)。
当出现‘@’时,清除本行中'@'前的所有字符(@本身不保留)。

Input
有若干行字符,行数不超过200.
(不存在单行只有#或@的字符串)

Output
输出的是处理过后的字符。
行数和输入一样。

Sample Input
whli##ile(ch=#!=eof###EOF)
{
   PRINT@printf("\n");
}

Sample Output
while(ch!=EOF)
{
printf("\n");
}

Hint
使用栈实现
<stack>

水题,栈的简单应用,想的是在STL方面运用得如流水一般,所以练练这些STL水题
#include<iostream>#include<stack>#include<cstdio>#include<cstring>using namespace std;int main(){    stack<char>q;    char c,s[105],z[105];    int i,l;    while(scanf("%s",z)!=EOF)    {        l=strlen(z);        for(i=0;i<l;i++)        {            if(z[i]=='#') q.pop();            else if(z[i]=='@')            {                while(!q.empty()) q.pop();            }            else q.push(z[i]);        }        i=0;        while(!q.empty())        {            s[i++]=q.top();            q.pop();        }        for(int j=i-1;j>=0;j--)            cout<<s[j];        cout<<endl;    }    return 0;}