跳到正文

题解

题解:AT_abc370_c [ABC370C] Word Ladder

Bychance约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v1当前

题意简述

题目会给出两个相同长度字符串 ss 和 tt,我们需要把 ss 变成 tt,每次只能改变一个字符,并把此时的 ss 添加到字符串 xx 的末尾,并在最后输出 xx。

需要注意的是,我们需要求得的是一个字典序最小的 xx。

思路简述

我们可以考虑贪心,因为我们需要的是最后的 xx 字典序最小,我们可以把他转化成每一步的改变都是当前最优的选择。

具体步骤如下:

  • 从左往右遍历字符串 ss。
  • 设当前位置为 ii,如果 si>tis_i>t_i,那么就改变此字符,因为此时的选择一定是最优的(因为我们是从左向右遍历的)。
  • 如果没有一个 0≤i<n0 \le i < n,使得 si>tis_i>t_i 那么我们就改变最后一个 0≤i<n0 \le i < n,使得 si≠tis_i \not = t_i,易证这个改变也是当前最优的。
  • 重付上述操作,知道对于所有的 0≤i<n0 \le i < n,都有 si=tis_i = t_i。

参考代码

C++
#pragma G++ optimize(3, "Ofast", "inline")
#pragma G++ optimize(2)
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <climits>
#include <random>
#include <bitset>
#include <unordered_map>
#define $ putchar('\n');
#define INF (1e18+3)
#define C(y,x) fac[y]*inv_fac[(y)-(x)]*inv_fac[x]
#define _max(a,b) ((a)>(b)?(a):(b))
#define _min(a,b) ((a)<(b)?(a):(b))
#define orz %
#define ll long long
#define int long long
#define pii std::pair<int, int>
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define m_p std::make_pair
#define pq std::priority_queue<int>
#define pq_min std::priority_queue<int, std::vector<int>, std::greater<int> >
#define pq_min_pii std::priority_queue<pii, std::vector<pii>, std::greater<pii> >
#define open(x); freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define test(x); cout << "Test: " << x << '\n'
#define close fclose(stdin);fclose(stdout);
#define ull unsigned long long
#define debug(); printf("Skmqwq\n");
#define ropen(x); freopen(#x".in", "r", stdin);
#define wopen(x); freopen(#x".out", "w", stdout);

namespace Fast_Skm {

	template <typename T>
	inline void read(T &x) {
		T s = 0, w = 1;
 		char c = getchar();
		while(!isdigit(c)) {
			if(c == '-')  w  = -1;
			c = getchar();
		}
		while(isdigit(c))
			s = (s << 1) + (s << 3) + (c & 0xcf), c = getchar();

		x = s * w;
		return ;
	}

	template <typename T, typename... Arp>
	inline void read(T &x, Arp &...arp) {
		read(x), read(arp...);
        return ;
	}

	template <typename T>
	inline void write(T x) {
		if(x < 0) x = -x, putchar('-');
		int p = 0;
		static char s[100];
		do {
			s[++p] = x orz 10 + '0';
			x /= 10;
		} while (x);
		while(p) putchar(s[p--]);
		putchar(' ');
	}

	template <typename T, typename... Arp>
	inline void write(T &x, Arp &...arp) {
		write(x), write(arp...);
		return ;
	}

	template <typename S, typename T>
	inline void smax(S &x, T y) {
		x = (x > y) ? x : y;
	}

	template <typename S, typename T>
	inline void smin(S &x, T y) {
		x = (x < y) ? x : y;
	}

	inline void quit() {
		exit(0);
		return ;
	}

	inline ll quick_pow(ll a, ll b, ll P) {
		ll ans = 1;
		while(b >= 1) {
			if(b & 1) {
				ans = ans * a % P;
			}
			a = a * a % P;
			b >>= 1;
		}
		return ans;
	}

	template <typename T>
	inline T exgcd(T a, T b, T &x, T &y) {
		if(b == 0) {
		 	x = 1; y = 0;
		 	return a;
		}
		int gcd = exgcd(b, a % b, x, y);
		int tmp = y;
		y = x - a / b * y;
		x = tmp;
		return gcd;
	}

} using namespace Fast_Skm;

const int N = 1e3 + 7;
int last = -1, cnt = 0;
bool f[N];//用来记录当前位置是否相同
std::string s, t;

signed main() {

	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);

	std::cin >> s >> t;
	for (int i = 0; i < s.size(); ++i) {
		if (s[i] != t[i]) {
			f[i] = 1; //初始化
			last = i, ++cnt;
		}
	}

	if (last == -1) {
		std::cout << 0;
		return 0;
	}

	std::cout << cnt << '\n';

	while (1) {
		bool flag = 1;
		for (int i = 0; i < s.size(); ++i)
			if (s[i] != t[i])
				last = i; //记录最后一个不相同的位置
		if (last == -1)
			break; //全部相同则跳出循环
		for (int i = 0; i < s.size(); ++i) {
			if (f[i] && flag) {
				if (s[i] > t[i] || i == last) {
					for (int j = 0; j <= i - 1; ++j)
						std::cout << s[j];
					std::cout << t[i];
					for (int j = i + 1; j < s.size(); ++j)
						std::cout << s[j];
					flag = 0;
					f[i] = 0;
					s[i] = t[i];
				}
			}
		}
		std::cout << '\n';
		last = -1;
	}

	return 0;
}
原文