字符串的走位(Z字形变换)

题目描述:输入字符串s和行数numRows,用空格隔开。对于给定的这个字符串,我们从上往下,从左往右进行И字形排列,排列好后再从左往右,从上往下将字符串重新排列,输出重新排列好的字符串。

如下例:输入"KONOHAGAKURENOSATO 3"

那么从左到右,从上到下再排序后就应该输出"KHKNTOOAAUEOAONGRS"

如果numRows为1则直接输出字符串

具体实现方法:

将字符串用string str正常储存。在草稿上И字形排列后,可以观察到,对于第一行和最后一行,相邻两元素的下标之差为定值,且与numRows有关,即下标都+(numRows - 1) * 2;而除了这两行之外,中间的每一行除了要+(numRows - 1) * 2之外,在+(numRows - 1) * 2之前还要将下标 +(numRows - 1 - i) * 2(其中i为行数 - 1)。

主要实现代码如下:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	string str = "";
	int numRows = 0;
	cout << "请输入字符串s和最大行数numRows: ";
	cin >> str >> numRows;
	/*错误检验略*/
	/*主要实现过程*/
	if (numRows == 1)
		cout << str;  // 防止后面for循环+=0
	vector<char> result;
	for (int i = 0; i <= numRows - 1; ++i)
	{
		if (i % (numRows - 1) == 0)  // 第一行或最后一行
			for (int vi = i; vi <= int(str.size()) - 1; vi += (numRows - 1) * 2)
				result.push_back(str[vi]);
		else
		{
			for (int vi = i; vi <= int(str.size()) - 1; vi += (numRows - 1) * 2)
			{
				result.push_back(str[vi]);
				if (vi + (numRows - 1 - i) * 2 <= int(str.size()) - 1)  // 保证不越界
					result.push_back(str[vi + (numRows - 1 - i) * 2]);
			}
		}
	}
	for (vector<char>::iterator it = result.begin(); it != result.end(); ++it)
		cout << *it;
	return 0;
}

除此方法外,还可以开辟一个二维数组用来模拟改序行为,但由于输入字符串长度不确定,所以用二维vector,然而此方法过于繁琐且浪费空间严重,故不推荐。

相关代码如下: 

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int iTCounter = 0,iStr=0,numRows=0;
	string str="";
	cout << "请输入字符串s和最大行数numRows: ";
	/*错误检验略*/
	// 当最大行数为1时,直接输出,为了防止在后续出现除数为零的情况
	if (numRows == 1)
	{
		cout << str;
		return 0;
	}
	/********************************************************
	  整体思路:开辟一个二维vector 名为TwoDimenTemp,用来存放
	          排列后的字符串str。为了方便编写程序,走位的顺序
			  改成从左至右,从下往上。如此一来,最后重新编排时
			  就是从上往下,从左至右
	*********************************************************/
	vector<vector<char> > TwoDimenTemp;
	vector<char> OneRow(numRows);  
    // 一维vector用来存放每一行的字符,存好一行后添加到二维vector里
	for (vector<char>::iterator it = OneRow.begin(); it != OneRow.end(); ++it)
		*it = '\0';
	while (iStr != int(str.size()))
	{
	// iTCounter表示二维数组列向的计数器,当其是numRows-1的整数倍时OneRow基本全有字符
		if (!(iTCounter % (numRows - 1)))  
		{
			int i = 0;
			while (str[iStr] != '\0')
			{
				OneRow[i] = str[iStr];
				iStr++;
				i++;
				if (i == numRows)
					break;
			}
			TwoDimenTemp.push_back(OneRow);  // 将OneRow添加到二维vector里
			for (vector<char>::iterator it = OneRow.begin(); it != OneRow.end(); ++it)
				*it = '\0';
		}
		else // 当iCounter不是numRows-1的整数倍时,OneRow只有一个有效字符
		{
			OneRow[numRows - 1 - iTCounter % (numRows - 1)] = str[iStr++];
			TwoDimenTemp.push_back(OneRow); 
			OneRow[numRows - 1 - iTCounter % (numRows - 1)] = '\0';
		}
		iTCounter += 1;
	}
	vector<char> svec(int(str.size()));  // 此vector里储存的是最后的结果
	vector<char>::iterator it = svec.begin();
	// 从上往下,从左至右遍历,二维vector的大小是添加的vector的个数(列向)
	for (int j = 0; j <= numRows - 1; ++j)
		for (int i = 0; i <= int(TwoDimenTemp.size()) - 1; ++i)
			if (TwoDimenTemp[i][j] != '\0')
			{
				*it = TwoDimenTemp[i][j];
				it++;
			}
	for (vector<char>::iterator it = svec.begin(); it != svec.end(); ++it)
		cout << *it;
	return 0;
}

2022年3月25日

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值