【GCJ2016】 round 1A

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值