跳到正文

题解

题解:AT_abc225_f [ABC225F] String Cards

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

文章信息

发布
2025年6月30日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

多头讲课讲的例题。

首先我们要解决一个问题,选出来所有卡牌之后如何排列是最小的?

我们借用 P1202 的思路:对于两串 a,ba,b,若 a+b<b+aa+b<b+a,则称 𝑎𝑎 小于 𝑏𝑏。

然后进行 DP。

首先第一个想到的思路是设 𝑑𝑝𝑖,𝑗𝑑𝑝_{𝑖,𝑗} 表示决定了前 ii 个元素,已经选择了 jj 个元素,此时能拼成的字典序最小的子序列是什么。

但其实这是不对的,因为当 𝑎<𝑏𝑎<𝑏 时,不一定有 𝑎+𝑐<𝑏+𝑐𝑎+𝑐<𝑏+𝑐,但一定有 𝑐+𝑎<𝑐+𝑏𝑐+𝑎<𝑐+𝑏。

所以我们应当倒着 DP。

设 𝑑𝑝𝑖,𝑗𝑑𝑝_{𝑖,𝑗} 表示决定了后 𝑖𝑖 个元素,已经选择了 𝑗𝑗 个元素,此时能拼成的字典序最小的子序列是什么。

然后这题就做完了。

Code:

C++
//私は灰の魔女イレーナです
#include<bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define Rep(i,a,b) for (int i=(a);i>=(b);i--)
#define grep(i,a,b) for (auto i : a[b])
using namespace std;
namespace fast_IO {
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
	#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
	#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
	#define endl '\n'
} using namespace fast_IO;
const int N=50+10;

int n,m,e[N];
string s[N],f[N][N],tmp;//后 i 个卡牌中,选了 j 个且一定选了 i 的最小字典序。
inline bool cmp(string a,string b) {//大:1 小:0
    return a>b;
}
inline bool cmp1(string a,string b) {//大:0 小:1
    if (a+b==b+a) return a.size()<b.size();
    if (a+b>b+a) return 0;
    return 1;
}
signed main() {
	cin>>n>>m;
    rep(i,1,n) cin>>s[i];
    sort(s+1,s+n+1,cmp1);
    rep(i,1,n*50) tmp+="z";
    rep(i,1,n) f[i][1]=s[i];
    rep(k,2,m) {
        Rep(i,n-k+1,1) {
            string minn=tmp;
            rep(j,i+1,n-k+2) {
                if (!cmp(minn,f[j][k-1])) continue;
                minn=f[j][k-1];
            }
            f[i][k]=s[i]+minn;
        }
    }
    string ans=tmp;
    rep(i,1,n-m+1) ans=min(ans,f[i][m]);
    cout<<ans<<endl;
	return (0-0);
}
原文
存档评论2 条抓取于