UVA 11988 - Broken Keyboard (a.k.a. Beiju Text)

简单的题,要用链表,保持时间复杂度在O(n),切记不要用数组、字符串,这道题目的时间卡的很紧,多次赋值会超时的。

源代码如下:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;

typedef struct node{
	char data;
	struct node*next;
	node(char a){
		data = a;
		next = NULL;
	}
}node;

int main(){
	string s;
	while (getline(cin, s)){
		int j;
		for (j = 0; j < s.size(); j++){
			if (s[j] != '['&&s[j] != ']') break;
		}
		node *head = new node(s[j]);
		node *tail = head;
		for (int i = j+1; i < s.size();){
			if (s[i] == '['){
				while (i < s.size()&&s[i]=='['){
					i++;
				}
				if (i == s.size()) break;
				if (s[i] != ']'){
					node* newhead = new node(s[i]);
					node* temp = newhead;
					i++;
					while (i < s.size() && s[i] != '['&&s[i] != ']'){
						temp->next = new node(s[i]);
						i++;
						temp = temp->next;
					}
					temp->next = head;
					head = newhead;
				}
			}
			else if (s[i] == ']'){
				while (i < s.size() && s[i] == ']') i++;
				if (s[i] != '['){
					while (i < s.size() && s[i] != '['&&s[i] != ']'){
						tail->next = new node(s[i]);
						i++;
						tail = tail->next;
					}
				}
			}
			else{
				tail->next = new node(s[i]);
				tail = tail->next;
				i++;
			}
		}
		while (head != NULL){
			cout << head->data;
			head = head->next;
		}
		cout << endl;
	}
	//system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值