ACM-行编辑程序

Description

如果遇到‘#’,表示后退一格,即前一字符无效,如果遇到@,表示前一单词无效,即退出到空格或所在行头为止。采用栈实现。

Input

输入包含若干行,由各种字符构成。

Output

利用描述规则输出最后的文本内容。

Sample Input

whli##ilr#e(s# *s)
   outcha@putchar( *s =# ++)

Sample Output

while( *s)
   putchar( *s  ++)

     个人比较喜欢第二种,好理解。

代码来自:http://blog.sina.com.cn/s/blog_b59d8e000101gts1.html
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
    char a[1010];
    int j;
    while(gets(a)!=NULL)
    {
        char b[1010];
        int index=0;
        for(int i=strlen(a)-1; i>=0; i--)
        {
            if(a[i]=='#')
            {
                int k=0;
                for(int j=i-1; j>=0; j--)
                {
                    if(a[j]!='#')
                    {
                        i=j;
                        break;
                    }
                    else
                        k++;
                }
                i = i-k;      
                continue;
            }
            else if(a[i]=='@')
            {
                for( j=i-1; j>=0; j--)
                {
                    if(a[j]==' ')
                    {
                        i=j+1;
                        break;
                    }
                }
                if(j>0)
                    continue;
                else
                    break;
            }
            else
                b[index++] = a[i];
        }
        for(int i=index-1; i>=0; i--)
        {
            printf("%c",b[i]);
        }
        printf("\n");
    }
    return 0;
}
代码来自:http://blog.sina.com.cn/s/blog_760c54300100qouj.html
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
       char c[10000],ch;
       int len,i;
       while(scanf("%c",&ch)!=EOF)
       {
              if(ch=='\n')
                     cout<<endl;
              else
              {
                     c[0]=ch;
                     len=1;
                     while(scanf("%c",&ch))
                     {
                            if(ch=='\n')
                            {
                                   for(i=0;i<len;i++)
                                          cout<<c[i];
                                   cout<<endl;
                                   break;
                            }
                            else if(ch=='#'&&len>0)
                                          len--;
                            else if(ch=='@')
                                   while(len>0)
                                   {
                                          if(c[len-1]==' ')
                                                 break;
                                          else
                                                 len--;
                                   }
                            else
                                   c[len++]=ch;
                     }
              }
       }
       return 0;
}

posted @ 2017-08-11 13:15  让你一生残梦  阅读(194)  评论(0编辑  收藏  举报