UVA12265-Selling Land(细节处理)

时间:2022-02-14 00:31:34
Problem UVA12265-Selling Land

Accept: 309  Submit: 3231
Time Limit: 3000 mSec

UVA12265-Selling Land(细节处理) Problem Description

UVA12265-Selling Land(细节处理)

UVA12265-Selling Land(细节处理) Input

There may be multiple test cases in the input. Each test case will begin with an even integer n (2 ≤ n ≤ 1,000) on its own line. On the next n lines will be names, one per line. Each name will be a single word consisting only of capital letters and will be no longer than 30 letters. The input will end with a ‘0’ on its own line.

UVA12265-Selling Land(细节处理) Output

For each case, print a single line containing the shortest possible string (with ties broken in favor of the alphabetically smallest) that your host could use to separate her guests. The strings should be printed in all capital letters.
 

UVA12265-Selling Land(细节处理) Sample Input

4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
2
LARHONDA
LARSEN
0
 

UVA12265-Selling Land(细节处理) Sample Output

K

FRED

JF

LARI

题解:这个题的细节真的很多,分析之后我采用了如下的方式,首先排序然后取中间两个串这个肯定没啥说的,定义一个ans空串,每次先加入小串的一个字符,如果这个字符小于大串的对应字符,这时答案就能构造出来了,构造方式如下:如果这个字符不是大串的最后一个字符或者这个字符对应的位置(大串-小串)>1,那么如果这个字符不是小串的最后一个字符,ans中该位置的字符++,输出,如果是最后一个,直接输出ans。否则看小串的剩下的字符是否是Z,直到找到一个不是Z的或者到小串的末尾,如果不是Z的字符不是小串的末尾,那么++输出,否则直接输出,如果到了末尾,在后面加一个A输出。这个分支的构造就结束了,如果比到最后也没有找到一个能比出大小的字符,那么就意味着直接输出小串即可。(描述起来太乱,看代码比较好)。

 #include<bits/stdc++.h>

 using namespace std;

 int n;
vector<string> str; int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (~scanf("%d", &n) && n) {
str.clear();
string tmp;
for (int i = ; i < n; i++) {
cin >> tmp;
str.push_back(tmp);
} sort(str.begin(), str.end());
string lmid = str[n / - ], rmid = str[n / ];
int llen = lmid.length(), rlen = rmid.length(); bool ok = false;
int i = ;
string ans = "";
while (i < llen && i < rlen) {
ans += lmid[i++];
if (lmid[i - ] < rmid[i - ]) {
if (i != rlen || rmid[i - ] - lmid[i - ] != ) {
if(i != llen) ans[i - ]++;
cout << ans << endl;
ok = true;
break;
}
else {
while (i < llen) {
ans += lmid[i++];
if (ans[i - ] != 'Z') {
if (i != llen)ans[i - ]++;
cout << ans << endl;
ok = true;
break;
}
} if (!ok) {
if(i != llen) ans += 'A';
cout <<ans << endl;
ok = true;
}
break;
}
}
} if (!ok) {
cout << lmid << endl;
}
}
return ;
}