首页 > 其他 > 详细

哈理工 oj——Simple Line Editor

时间:2015-04-12 09:18:15      阅读:158      评论:0      收藏:0      [点我收藏+]

Simple Line Editor

Time Limit: 1000 MS

Memory Limit: 65536 K

Total Submit: 573(151 users)

Total Accepted: 183(139 users)

Rating: 技术分享技术分享技术分享

Special Judge: No

Description

Early computer used line editor, which allowed text to be created and changed only within one line at a time. However, in line editor programs, typing, editing, and document display do not occur simultaneously (unlike the modern text editor like Microsoft Word). Typically, typing does not enter text directly into the document. Instead, users modify the document text by entering simple commands on a text-only terminal.

 

Here is an example of a simple line editor which can only process English. In addition, it has two commands. ‘@’ and ‘#’. ‘#’ means to cancel the previous letter, and ‘@’ is a command which invalidates all the letters typed before. That is to say, if you want type “aa”, but have mistakenly entered “ab”, then you should enter ‘#a’ or ‘@aa’ to correct it. Note that if there is no letter in the current document, ‘@’ or ‘#’ command will do nothing.

Input

The first line contains an integer T, which is the number of test cases. Each test case is a typing sequence of a line editor, which contains only lower case letters, ‘@’ and ‘#’.

Output

For each test case, print one line which represents the final document of the user. There would be no empty line in the test data.

Sample Input

2
ab#a
ab@aa

Sample Output

aa
aa

 

想着用 两个方法做,用STL做的时候 ,好多容器细节,都搞不懂,把这些细节查懂了后,做起来如鱼得水。

vector容器来做:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
	string str;
	int T;
	cin>>T;
	while(T--)
	{
		cin>>str;
		vector<char>ls;
		for(int i=0;i<str.size();i++)
		{
			if(((str[i]=='#')||(str[i]=='@'))&&(ls.size()==0))
			continue;
			
			else if(str[i]=='@')
				ls.clear();
			else if(str[i]=='#')
				ls.pop_back();

			else 
			{
				ls.push_back(str[i]);
				
			}
		}
		for(vector<char>::iterator iter=ls.begin();iter!=ls.end();iter++)
			cout<<*iter;
		cout<<endl;
		ls.clear();
		
	}
	return 0;
}


 

用刚学的栈来做!

更加顺利!要注意每次pop()之前都要检查 栈是否为空。

AC:

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main()
{
	int T;cin>>T;
	string str;
	while(T--)
	{
		stack<char>gq;
		cin>>str;
		for(int i=0;i<str.size();i++)
		{
			if(str[i]!='#'&&str[i]!='@')
				gq.push(str[i]);
			else if(str[i]=='#'&&!gq.empty())
				gq.pop();
			else if(str[i]=='@'&&!gq.empty())
			{
				while(!gq.empty())
				{
					gq.pop();
				}
			}
		}
		int dic=0;char cnt[100000];
		while(!gq.empty())
		{
			cnt[dic++]=gq.top();
			gq.pop();
		}
		for(int t=dic-1;t>=0;t--)
			cout<<cnt[t];
		cout<<endl;

	}
	return 0;
}


 

哈理工 oj——Simple Line Editor

原文:http://blog.csdn.net/lsgqjh/article/details/44999243

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!