Broken Keyboard UVA - 11988 链表

Broken Keyboard UVA - 11988 链表
添加链接描述

此题要注意的点:
(1)要有固定的链表头,否则字母插入头后,找不到头,代码固定的头0,该空间没有存值
(2)当前字符要插入的位置,是有上个字符决定的,所以扫描到一个字符,cur指示的是下一个字符要插入的位置
(3)要记录last,因为又’[‘,插入到链表尾和其他有所不同
注意:
1.char text[]尚未赋值之前都是’\0’,strlen从当前字符开始算,直到末尾,但是不包括末尾的’\0’
2.strlen 不要放在for循环里面,否则会超时

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

int last;
int cur;  //cur记录当前字符要插入在cur坐标的后面 

char text[100005];
int nextt[100005];

int main()
{
	
	while(scanf("%s",text+1)==1)
	{
		int len=strlen(text+1);  //!!!!!strlen 不要放在for循环里面,否则会超时 
		cur=0;
		last=0;
		nextt[0]=0;		
		for(int i=1;i<=len;i++)
		{
			if(text[i]=='[')
			{
				cur=0;
			}else if(text[i]==']')
			{
				cur=last;
			}else{
				if(cur==last)   //如果要插在last要单独处理 
				{
					nextt[cur]=i;
					last=i;
				}else {
					nextt[i]=nextt[cur];
					nextt[cur]=i;
				}
				cur=i;   //记得更新cur,[ste,因为te还有跟在s后面 
			}
		}
		int m=nextt[0];
		while(m!=last)
		{
			cout<<text[m];
			m=nextt[m];
		}
		cout<<text[last];
		cout<<endl;
	}
	
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值