POJ 1706

此题目我大量运用C++中的string算法和容器。思路简单。但是在POJ中出现Running time,在本地调试通过。环境是vs2010

// POJ1706(引用).cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
void correct(vector<string> &reference,int number,int count)
{
	
	vector<string>::iterator it;
	for(it=reference.begin ();it!=reference.end();it++)
	{
		int pos=it->find("]");
		int num=pos-1;
		string sub=(*it).substr (1,num);
		stringstream p;
		p<<sub;
		int pos_num;
		p>>pos_num;
		p.clear ();
		p<<count;
		p>>sub;
		if(pos_num==number)
		{
			(*it).replace(1,num,sub);
			break;
		}

	}
}
void find_para(vector<string> &reference,vector<string> ¶graph)
{
	
	vector<int> s(100,0);
	vector<string>::iterator  it;
	int count=0;
	for(it=paragraph.begin();it!=paragraph.end();it++)
	{
		string::size_type pos1=0;
		string::size_type pos2=0;
		
		
		while((pos1=it->find("[",pos2))!=string::npos)
		{
			 pos2=it->find("]",pos2);
			int num=pos2-pos1;
			string sub=(*it).substr (pos1+1,num-1);
			stringstream p;
			p<<sub;
			int pos_num;
			p>>pos_num;
			if(s[pos_num]==0)
			{
				count++;
				s[pos_num]=count;
				correct(reference,pos_num,count);
			}
			else
			{
				count=s[pos_num];
			}
			p.clear ();
			p<<count;
			p>>sub;
			(*it).replace (pos1+1,num-1,sub);
			
			pos2+=1;
		}
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	freopen("test.txt","r",stdin);//读入源文件
	vector<string> reference;
	vector<string> paragraph;
	string s1,s2,s3;
	while(getline(cin,s2))
	{
		s3="";
		s1=s2;
		do
		{
			if(!s1.empty ())
				s3+=s1+'\n';
			else break;
		}while(getline(cin,s1));
		if(s3!="")
		{
			if(s3[0]=='[')
				reference.push_back (s3);
			else
				paragraph.push_back (s3);
		}
	}
	find_para(reference,paragraph);
	sort(reference.begin (),reference.end());
	vector<string>::iterator it;
	
	for(it=paragraph.begin ();it!=paragraph.end();it++)
		cout<<*it<<endl;
	cout<<endl;
	for(it=reference.begin ();it!=reference.end();it++)
		cout<<*it<<endl;
	cout<<endl;
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值