https://vjudge.net/problem/UVA-1610
题目
给偶数个互不相同的仅包含大写字母的字符串,要求找出最短的仅包含大写字母的字符串,使一半的字符串小于等于它,一半的字符串大于它。
字符串A小于字符串B有以下情况:
1.$K=\min\{len(A),len(B)\}, \exists 0\leqslant i < K, \forall 0<=x<i, A[i]<B[i] and a[x]=b[x]$
2.$K=\min\{len(A),len(B)\}, \forall 0<=x<K, a[x]=b[x] and len(A)<len(B)$
其中大写字母从小到大顺序为“ABCDEFGHIJKLMNOPQRSTUVWXYZ”= =
题解
又是斗智题……
改了很多次……策略很琐碎,不想解释了……
这道题应该先写暴力搜索,再对拍,这样写起来才快!
字符串长度不确定,因此应该使用string。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...)
#endif
#define MAXN 1007
string k[MAXN];
int main() {
#ifdef sahdsg
// freopen("in.txt", "r", stdin);
#endif
int n;
while(cin>>n && n) {
REP(i,0,n) {
cin >> k[i];
}
sort(k,k+n);
int f=(n-1)/2;
//f f+1
int l1 = k[f].length(), l2 = k[f+1].length();
if(l1>l2) {
string ans; ans=k[f];
REP(i,0,l2) {
if(k[f][i]+1<k[f+1][i]) {
ans[i]=k[f][i]+1;
ans[i+1]=0;
break;
} else if(k[f][i]+1==k[f+1][i]) {
if(i==l2-1)
for(i++; i<l1-1; i++) {
if(ans[i]<'Z') {
ans[i]=k[f][i]+1;
ans[i+1]=0;
break;
}
}
else {
ans[i]=k[f+1][i];
ans[i+1]=0;
}
break;
}
}
puts(ans.c_str());
} else { //l1<=l2 s1<s2
string ans; ans=k[f];
int l=k[f].length();
REP(i,0,l1-1) {
if(k[f][i]<k[f+1][i]) {
ans[i]=k[f][i]+1;
ans[i+1]=0;
break;
}
}
puts(ans.c_str());
}
}
return 0;
}
对拍代码
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include<bits/stdc++.h>
using namespace std;
FILE *f;
int main() {
srand(time(NULL) ^ (unsigned long long)new char());
while(1) {
f= fopen("in.txt", "w");
fputc('2', f); fputc('\n', f);
int k=rand()%100;
for(int _=0; _<k; _++) {
fputc('A'+rand()%26, f);
}
fputc('\n', f);
k=rand()%100;
for(int _=0; _<k; _++) {
fputc('A'+rand()%26, f);
} fputc('\n', f);
fclose(f);
putchar('.');
system("oj < in.txt > out.txt");
putchar('.');
system("bl < in.txt > out2.txt");
printf("[v]");putchar('\n');
if (system("fc out.txt out2.txt")) {
putchar('!');
putchar('\a');
getchar();
}
}
return 0;
}