1140 Look-and-say Sequence——非字符串做法(很一般的做法)

分析

遍历序列,记录 连续 相同的数字
生成下一个序列(记录 连续 相同的数字)
直到递归次数为n

具体来说

  • 递归

由于是参照上一个序列,生成下一个序列,同时作为下一次递归的“参照”

所以需要2个vector,递归时交换次序

  • 搜索

由于数字是固定【0,9】,因此做个数表,找连续 相同的数字,就从数表里搜索

 

其他注意

由于不知道最终的序列长度是多少,因此设为尽可能长

在测试下,至少要100000(反正我从1000开始测试,测试点6过不去,100000可以)

 

代码

#include<bits/stdc++.h>
using  namespace std;

int key, length, times{ 1 };
vector<int>T{ 0,1,2,3,4,5,6,7,8,9 };//固定常数(表)用大写

void InPut(vector<int>&, vector<int>&);
void Recursive(vector<int>, vector<int>);
int main() {
	vector<int>table, table2;
	InPut(table, table2);
	Recursive(table, table2);
	return 0;
}
void InPut(vector<int>& table, vector<int>& table2) {
	scanf("%d %d", &key, &length);
	table.resize(100000);
	table2.resize(100000);
	fill(table.begin(), table.end(), -1);
	fill(table2.begin(), table2.end(), -1);
	table[0] = key;
}
void Recursive(vector<int> table, vector<int> table2) {
	if (times == length) {
		for (auto i : table) {
			if (i != -1)
				printf("%d", i);
		}
		return;
	}
	times++;
	int count, j, z{ 0 };
	for (int i = 0; i < table.size() && table[i] != -1; i = j) {
		count = 0;
		auto it = find(T.begin(), T.end(), table[i]);
		for (j = i; j < table.size() && table[j] == *it; j++) {
			count++;
		}
		table2[z] = *it;
		z++;
		table2[z] = count;
		z++;
	}
	Recursive(table2, table);
}

后记

这种解法确实很一般,甚至很糟糕;是没有柳神的string来的简洁的

仅是自己做时的第一反应,刚好也能应付这题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值