首页 > 代码库 > UVA - 11988Broken Keyboard (a.k.a. Beiju Text)(链表)

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

题目: UVA - 11988Broken Keyboard (a.k.a. Beiju Text)(链表)


题目大意:某位程序员在用坏掉的键盘打字,这个键盘的home键和end键会是不是自己打印。然后现在给出这样的一串文字,要求你打印出之后会在屏幕上显示的字符串。


解题思路:home键是跳到这一行的开头开始打印,end键是跳到这一行的末尾开始打印。用一个链表将home和end之后的字符串串起来,之后在按顺序输出就可以了。碰到home键后面的字符串(从当前的home到另一个home或是end或是结束符为止)放到链表的前面。end的放后面。


代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <list>

using namespace std;

string str;
list<string> l;
list<string>::iterator it;

void solve () {

	int n;
	string tmp;

	n = 0;
	for (int i = 0; i < str.length(); i++, n++)
		if (str[i] == ']' || str[i] == '[')
			break;
	
	tmp = "";
	tmp = str.substr (0, n);
	l.push_back(tmp);

	for (int i = 0; i < str.length(); i++) {
		
		if(str[i] == '[') {

			n = 0;
			tmp = "";
			for (int j = i + 1; j < str.length() && str[j] != ']' && str[j] != '['; j++)
				n++;
			tmp = str.substr(i + 1, n);
			l.push_front(tmp);
			i += n;

		} else if (str[i] == ']') {

			n = 0;
			tmp = "";
			for (int j = i + 1; j < str.length() && str[j] != '[' && str[j] != ']'; j++)
				n++;

			tmp = str.substr(i + 1, n);
			l.push_back(tmp);	
			i += n;
		} 
	}
}
/*
Print_it (const string &a) {

	cout<<a;	
}*/

int main () {
	
	while (cin>>str) {

		solve ();
	//	for_each (l.begin(), l.end(), Print_it);
		for (it = l.begin(); it != l.end(); it++)
			cout<<*it;
		cout<<endl;
		l.erase(l.begin(), l.end());
	}
	return 0;
}


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