题意:给出一个打字序列,其中[表示home键,]表示end键,处理一下,输出实际输出的序列。
思路:邻接表的思路,记录每个字母的下一个字母在的位置。
//UVA11988
//邻接表
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 100005
char s[100005];
char a[100005];
int head[maxn];
int main()
{
while(gets(s))
{
int cur=0,last=0,ee=0;
memset(head,-1,sizeof head);
for (int i = 0;s[i];i++)
{
if(s[i] == '[')
{
cur = 0;
}
else if(s[i] == ']')
{
cur = last;
}
else
{
ee++;
a[ee] = s[i];
head[ee] = head[cur];
head[cur] = ee;
cur = ee;
if(head[ee] == -1)last = ee;
}
}
for (int i = head[0];i!=-1;i = head[i])
{
printf("%c",a[i]);
}
puts("");
}
return 0;
}