数码串和oj

原代码:

复制代码
#include<stdio.h>
#include<string.h>
int min(int a,int b){
	return (a>b)?b:a;
}
int main(){
	int T;
	scanf("%d",&T);
	getchar();
	while(T--){
		char s[100005];
		scanf("%s",s);
		int len=strlen(s);
		if(len==1){
			printf("%s\n",s);
			continue;
		}
		int i=0;
		for(int d=0;d<len-1;d++){
			if(s[d]>s[d+1]){
				char temp[100005];
				if(d!=0)
				strncpy(temp,s,d-1);
				strncpy(temp+d,s+d+1,len-d-1);
				temp[len-1]=min(s[d]-'0'+1,9)+'0';
				temp[len]='\0';
				strcpy(s,temp);
			}
		}
		printf("%s\n",s);
	}
}

显然时间超限了,现在用空间换时间,引入整数数组,并且长度为len3倍

复制代码
#include<stdio.h>
#include<string.h>
#define maxsize 100000
int min(int a, int b) {
	if (a >= b) {
		return b;
	} else {
		return a;
	}
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		char str[maxsize];
		scanf("%s", str);
		int len = strlen(str);
		int arr[len * 9];
		for (int i = 0; i < len; i++) {
			arr[i] = str[i] - '0';
		}
		int z = len;

		for (int i = 0; i < z; i++) {
			if (arr[i] > arr[i + 1]) {
				arr[z] = min(arr[i] + 1, 9);
				z++;
				arr[i] = 11;
			}
		}
		for (int i = 0; i < z; i++) {
			if (arr[i] != 11) {
				printf("%d", arr[i]);
			}
		}
		printf("\n");
	}
	return 0;
}
相关推荐
鹿角片ljp1 天前
LeetCode 46. 全排列|吃透回溯
算法·leetcode·职场和发展
鼎艺创新科技1 天前
不依赖 UE/Unity:我们如何从零搭建一套国产三维 GIS 渲染引擎
人工智能·算法·unity·游戏引擎·三维电子沙盘
石头猫灯1 天前
WordPress wp2shell 漏洞链完整拆解流程
网络·数据结构·安全·web安全
.道阻且长.1 天前
11.LeetCode算法习题讲解--滑动窗口--将x减到0的最小操作数
算法·leetcode·职场和发展
wenyq71 天前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
.格子衫.1 天前
032动态规划之区间DP——算法备赛
算法·动态规划
小欣加油1 天前
leetcode3069 将元素分配到两个数组中I
数据结构·c++·算法·leetcode·职场和发展
不会代码的小猴1 天前
7. JSON
开发语言·c++·笔记·qt·算法·json
怪奇云呼军1 天前
知识库也会注入指令?闪电智能VoiceAgent 如何防住 Prompt Injection
人工智能·python·算法·云计算·音视频
阿里云大数据AI技术1 天前
基于 EMR Serverless Ray 实现 Qwen 模型批量推理实践
人工智能·算法·agent