置换索引

<p>
<span style="font-size:14px;"><strong>题目</strong>:</span>(Accelerated Ex5-1)</p><p>首先解释一下题目的意思,</p><p>对于输入</p><p>The quick brown fox</p><p>jumped over the fence</p><p><strong><span style="font-size: 14px;">然后首先将每一句进行轮换,得到(就光看第一句)</span></strong></p><p>The quick brown fox(这个索引是空)</p><p>quick brown fox The(这个索引是The)</p><p>brown fox The quick (这个索引是The quick)</p><p>fox The quick brown (这个索引是The quick brown 索引就是指已轮换部分</p><p><strong><span style="font-size: 14px;">然后按字典序排序,得到输出</span></strong></p><p>brown fox The quick</p><p>fox The quick brown</p><p>quick brown fox The</p><p>The quick brown fox</p><p>然后把索引放左边 其余放右边 排列整齐输出就行</p><p>
</p><p><span style="font-size: 14px;">我的具体实现并没有按照书上的3个步骤来做,我是先在每一行后加了个结尾标记,然后这个标记就是索引部分与其余部分的分隔符。我只用轮换完之后排完序,再按格式输出就行了。</span></p><p><span style="font-size: 14px;">
</span></p><p><span style="font-size: 14px;">具体代码(VS2010):</span></p>

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
#define ENDFLAG 127
typedef string::size_type StrSz;
void Read(vector<string>& str)
{
	string temps;
	while(getline(cin,temps))
	{
		str.push_back(temps);
	}
}
void MyFind(const string& cur,StrSz& indexSep,StrSz& indexAlnum,char& Sep)
{
	Sep=' ';
	indexSep=indexAlnum=cur.size();
	for(int i=0;i!=cur.size();++i)
	{
		if(cur[i]==' ' || cur[i]==ENDFLAG)
		{
			indexSep=i;
			
			if(cur[i]==ENDFLAG)
				Sep=ENDFLAG;
			int j=i+1;
			while(j!=cur.size())
			{
				if(isalnum(cur[j]))
				{
					indexAlnum=j;
					return;
				}
				++j;
			}
			break;
		}
	}
}
void Produce(string cur,vector<string>& rotateStr)
{
	//清除字符串末尾空格
	//如果文中出现标点符号 那应该算做多句 所以应该在之前进行分解
	int i=cur.size()-1;
	while( !isalnum(cur[i]) ) --i;
	cur.resize(i+1);
	//添加结尾标记 ASCII码最后一个字符
	cur+=(char)ENDFLAG;
	cout<<"Size:"<<cur.size()<<"  "<<cur<<endl;
	string temp;
	char Sep=0;
	while(Sep!=ENDFLAG)
	{	
		rotateStr.push_back(cur);
		StrSz indexSep=cur.size(),indexAlnum=cur.size(); //indexSep indexAlnum为了消除两个单词之间的空格
		MyFind(cur,indexSep,indexAlnum,Sep);   //找下一个分隔符的位置 找下一个单词的开头位置 找下一个分隔符是哪个
		temp = cur.substr(indexAlnum);
		cur.resize(indexSep);
		cur  = temp + cur + string(1,Sep);
	}
}
void MyRotate(const vector<string>& input,vector<string>& rotateStr)
{
	vector<string>::const_iterator iter=input.begin();
	while(iter!=input.end())
	{
		Produce(*iter,rotateStr);
		iter++;
	}
}
void MyPrint(vector<string>& rotateStr)
{
	vector<string> left,right;
	StrSz maxLeft=0,maxRight=0;
	vector<string>::iterator iter=rotateStr.begin();
	string temp;
	for(;iter!=rotateStr.end();++iter)
	{
		StrSz index=iter->find(ENDFLAG);
		right.push_back(iter->substr(index+1));
		maxRight=max(maxRight,right.back().size());
		iter->resize(index);
		left.push_back(*iter);
		maxLeft=max(maxLeft,left.back().size());
	}
	for(vector<string>::size_type i=0;i!=left.size();++i)
	{
		cout<<setw(maxRight)<<right[i];
		cout<<"    ";
		cout<<setw(maxLeft)<<left[i];
		cout<<endl;
	}
}

int main()
{
	vector<string> inStr;
	vector<string> rotateStr;
	Read(inStr);
	MyRotate(inStr,rotateStr);//rotateStr以引用的方式传入函数
    sort(rotateStr.begin(),rotateStr.end());
	MyPrint(rotateStr);
	while(1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值