UVA12265-Selling Land(细节处理)

Problem UVA12265-Selling Land

Accept: 309  Submit: 3231
Time Limit: 3000 mSec

Problem Description

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.

 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.
 

 Sample Input

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

 Sample Output

K

FRED

JF

LARI

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

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int n;
 6 vector<string> str;
 7 
 8 int main()
 9 {
10     //freopen("input.txt", "r", stdin);
11     //freopen("output.txt", "w", stdout);
12     while (~scanf("%d", &n) && n) {
13         str.clear();
14         string tmp;
15         for (int i = 0; i < n; i++) {
16             cin >> tmp;
17             str.push_back(tmp);
18         }
19 
20         sort(str.begin(), str.end());
21         string lmid = str[n / 2 - 1], rmid = str[n / 2];
22         int llen = lmid.length(), rlen = rmid.length();
23         
24         bool ok = false;
25         int i = 0;
26         string ans = "";
27         while (i < llen && i < rlen) {
28             ans += lmid[i++];
29             if (lmid[i - 1] < rmid[i - 1]) {
30                 if (i != rlen || rmid[i - 1] - lmid[i - 1] != 1) {
31                     if(i != llen) ans[i - 1]++;
32                     cout << ans << endl;
33                     ok = true;
34                     break;
35                 }
36                 else {
37                     while (i < llen) {
38                         ans += lmid[i++];
39                         if (ans[i - 1] != 'Z') {
40                             if (i != llen)ans[i - 1]++;
41                             cout << ans << endl;
42                             ok = true;
43                             break;
44                         }
45                     }
46 
47                     if (!ok) {
48                         if(i != llen) ans += 'A';
49                         cout <<ans << endl;
50                         ok = true;
51                     }
52                     break;
53                 }
54             }
55         }
56 
57         if (!ok) {
58             cout << lmid << endl;
59         }
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/npugen/p/9683233.html