1084 外观数列 (20 分)(算法分析+代码实现)(测试点四段错误)

74 阅读1分钟

1084 外观数列 (20 分)

题目链接

算法分析

循环处理字符串,每读到一个字符,就向后拓展是否重复,然后存入temp临时字符串,最后再赋值回来。

测试点

测试点四是一个大数据点,长度高达100000,修改一下就过了。

代码实现

#include<bits/stdc++.h>
using namespace std;
#define N 100000
char s[N], temp[N], d;
int cnt_s, cnt_temp, t;
int main(){
	int x;
	scanf("%c %d", &d, &x);
	s[++ cnt_s] = d;//最开始的字符串
	for(int i = 1; i <=  x - 1; ++ i){//处理x-1次
		for(int j = 1; j <= cnt_s; ++ j){
			++ t;
			temp[++ cnt_temp] = s[j];
			while(s[j] == s[j + 1]){//统计是否有重复字符的出现
				++ j;
				++ t;
			}
			temp[++ cnt_temp] = (char)(t + 48);//数字转字符必备
			t = 0;
		}
		for(int j = 1; j <= cnt_temp; ++ j)
			s[j] = temp[j];
		cnt_s = cnt_temp;
		cnt_temp = 0;
	}
	for(int i = 1; i <= cnt_s; ++ i)
		printf("%c", s[i]);
	return 0;
}