您的位置:首页 > 理论基础 > 数据结构算法

UVA11988 Broken Keyboard (a.k.a. Beiju Text)

2015-07-17 11:40 309 查看
题意:给出一个打字序列,其中[表示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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 UVA 邻接表