破损的键盘(codevs 4650)

题目描述 Description

有一天,你需要打一份文件,但是你的键盘坏了,上面的"home"键和"end"键会时不时地按下,而你却毫不知情,甚至你都懒得打开显示器,当你打开显示器之后,出现在你的面前的是一段悲剧的文本。

输入描述 Input Description

输入只有一行,即这份文件,这份文件只包含小写字母和'['以及']',用'['代替"home"键,用']'代替"end"键。

输出描述 Output Description

你的任务是在打开显示器之前,计算出这份悲剧的文档。

样例输入 Sample Input

kdg[gek]h[itj

de[co]vs

样例输出 Sample Output

itjgekkdgh

codevs

数据范围及提示 Data Size & Hint

包含多组测试数据,直到文件结束。

字符串长度小于10000个字符。

不包含空格。

分析:用’[’或’]’把字符串隔成几个部分,给几个部分按’[’或’]’附上编号,最后按编号排序输入。
     时间将近2000ms,空间也是擦边过,唏嘘一场。
 代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#define M 10010
using namespace std;
int front,behind;
char s[M];
struct node
{
    char ss[M/3];
    int num,len;
};node e[M/3];
bool cmp(const node&x,const node&y)
{
    return x.num<y.num;
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        memset(e,0,sizeof(e));
        front=behind=0;
        int len=strlen(s),cnt=1;
        e[1].num=0;
        for(int i=0;i<len;i++)
        {
            char c=s[i];
            if(s[i]=='[')
            {
                if(e[1].len)++cnt;
                e[cnt].num=--front;
            }
            else if(s[i]==']')
            {
                if(e[1].len)++cnt;
                e[cnt].num=++behind;
            }
            else e[cnt].ss[++e[cnt].len]=s[i];
        }
        sort(e+1,e+cnt+1,cmp);
        for(int i=1;i<=cnt;i++)
          for(int j=1;j<=e[i].len;j++)
            printf("%c",e[i].ss[j]);
        cout<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/harden/p/5785477.html