您的位置:首页 > 其它

【GCJ2016】 round 1A

2016-04-16 11:57 253 查看

A:The Last Word

题意是给一个字符串,依次取出一个字母,然后将这些字母组成新串,组合的规则,只能放在当前串的串首或者串尾。求字典序最大的新串。

好简单,直接每次判断新取出的字母与当前组成的串的首字母比,比首字母大加到首部,否则加到尾部。

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
//freopen("A-large.in", "r", stdin);
//freopen("outAL.txt", "w", stdout);
int T;
string str;
cin >> T;
for(int i = 1; i <= T; ++ i) {
cin >> str;
string ans = "";
ans += str[0];
int len = str.size();

for(int j = 1; j < len; ++ j) {
if(str[j] >= ans[0]) {
ans = str[j] + ans;
}
else ans = ans + str[j];
}
cout << "Case #" << i << ": " << ans << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}


B. Rank and File

提议是给出一个2 * n - 1行数,其中这些数矩阵中从左往右递增,从上到下依次递增的的矩阵中的行列。

如 :

1 2 3

2 3 5

3 4 6

这样的一个矩阵,那么给出

1 2 3

2 3 5

3 4 6

1 2 3

2 3 4

求出其中没有列出的那一行或那一列数。

细心的人会发现,矩阵中按照这种规则写出的2 * n行数每个数字出现必为偶数。所以只需要对着2 * n - 1用map保存一下,然后找到个数是奇数的数,最后排序下就得到结果了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <climits>
#include <unordered_map>

using namespace std;

int main()
{
//freopen("B-large.in", "r", stdin);
//freopen("outBL.txt", "w", stdout);
int n, t;
cin >> t;
for(int i = 1; i <= t; ++ i) {
cin >> n;
int num;
unordered_map<int, int> umap;
for(int j = 0; j < 2*n-1; ++ j)
for(int k = 0; k < n; ++ k)
{
cin >> num;
umap[num] ++;
}
unordered_map<int, int>::iterator itr;
vector<int> ans;
for(itr = umap.begin(); itr != umap.end(); ++ itr) {
if(itr->second & 1) ans.push_back(itr->first);
}
sort(ans.begin(), ans.end());
cout << "Case #" << i << ": " << ans[0];
for(int j = 1; j < ans.size(); ++ j)
cout << " " << ans[j];
cout << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: