【GCJ2016】 round 1A

来源:互联网 发布:怎么认识外国妹子软件 编辑:程序博客网 时间:2024/06/05 14:28

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;}
0 0