多头讲课讲的例题。
首先我们要解决一个问题,选出来所有卡牌之后如何排列是最小的?
我们借用 P1202 的思路:对于两串 ,若 ,则称 小于 。
然后进行 DP。
首先第一个想到的思路是设 表示决定了前 个元素,已经选择了 个元素,此时能拼成的字典序最小的子序列是什么。
但其实这是不对的,因为当 时,不一定有 ,但一定有 。
所以我们应当倒着 DP。
设 表示决定了后 个元素,已经选择了 个元素,此时能拼成的字典序最小的子序列是什么。
然后这题就做完了。
Code:
//私は灰の魔女イレーナです
#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);
}