#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 60, M = 1010;
int n;
int a[N];
int st[M];
stack<int> w;
int main()
{
int T;
cin >> T;
while (T -- )
{
memset(st, 0, sizeof st);
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i], st[a[i]] ++;
for (int i = n; i >= 1; i -- )
{
if(st[a[i]])
{
w.push(a[i]);
st[a[i]] = false;
}
}
cout << w.size() << endl;
while(w.size())
{
cout << w.top() << ‘ ‘;
w.pop();
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10, M = 30;;
int T, n, k;
char s[N];
bool st[M];
char a[N];
char ans[2];
char cos[2];
void f(char cos[])
{
int a = cos[0] - ‘a‘;
int b = cos[1] - ‘a‘;
bool flag = false; //能不能找到比b大的最小的字母
for (int i = b + 1; i < 26; i ++ )
{
if(st[i])
{
flag = true;
b = i; //更新b;
break;
}
}
if(!flag) //如果没找到
{
for(int i = a + 1; i < 26; i ++)
{
if(st[i])
{
a = i;
break;
}
}
for(int i = 0; i < 26; i ++)
{
if(st[i])
{
b = i;
break;
}
}
}
ans[0] = a + ‘a‘;
ans[1] = b + ‘a‘;
}
int main()
{
cin >> T;
while (T -- )
{
memset(st, false, sizeof st);
scanf("%d%d%s", &n, &k, s);
for(int i = 0; i < n; i ++) st[s[i] - ‘a‘] = true;
if( n < k )
{
char c; //找最小的
for (int i = 0; i < 26; i ++ )
{
if(st[i])
{
c = i + ‘a‘;
break;
}
}
for (int i = 0; i < n; i ++ ) a[i] = s[i];
for (int i = n; i < k; i ++) a[i] = c;
}
else
{
if(k == 1)
{
char c;
int idx = 0;
int x = s[0] - ‘a‘;
for (int i = x + 1; i < 26; i ++ )
{
if(st[i])
{
c = i + ‘a‘;
break;
}
}
a[0] = c;
}
else
{
//找最大的
char m;
for (int i = 25; i >= 0; i -- )
{
if(st[i])
{
m = i + ‘a‘;
break;
}
}
// 找最小的
char h;
for (int i = 0; i < 26; i ++ )
{
if(st[i])
{
h = i + ‘a‘;
break;
}
}
//找p
int p;
for(int i = k - 1; i >= 0; i --)
{
if(s[i] != m)
{
p = i; //找到p
break;
}
}
if(p == k - 1) p --;
for (int i = 0; i < p; i ++ ) a[i] = s[i];
for (int i = p + 2; i < k; i ++ ) a[i] = h;
cos[0] = s[p], cos[1] = s[p + 1];
f(cos);
a[p] = ans[0];
a[p + 1] = ans[1];
}
}
//cout << ans[0] << ‘ ‘ << ans[1] << endl;
for (int i = 0; i < k; i ++ ) cout << a[i];
cout << endl;
}
return 0;
}
AcWing第12场周赛-2021/8/14