题目链接
A.DNA Sorting
题面
思路
题目意思就是说,如果一个字符串中前面的字符比后面的字符大,那么它的无序度就+1,根据这个给一组字符串从最有序到最无序依次输出。那么明白题目意思之后直接模拟即可。
示例代码
#include<bits/stdc++.h>using namespace std;#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << '\n'const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;signed main() {ios::sync_with_stdio(false); cin.tie(nullptr);int n, m;cin >> n >> m;vector<pair<int, string>> arr(m);fer(i, 0, m) cin >> arr[i].second;for(auto &p : arr){string s = p.second;fer(i, 0, s.size()){fer(j, i + 1, s.size()) p.first += (s[i] > s[j]);}}sort(all(arr));for(auto p : arr) cout << p.second << '\n';return 0;
}
B.Integer Inquiry
题面
思路
其实就是求多个数的高精度加法
示例代码
ans = 0
while True:n = int(input())if n == 0:breakans += n
print(ans)
#include<bits/stdc++.h>using namespace std;#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << '\n'const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;string add(string s1, string s2) {string res;vector<int> a(110), b(110), c(110);int n1 = s1.size(), n2 = s2.size();for(int i = 0, j = n1 - 1; i < n1; i++, j--) a[i] = s1[j] - '0';for(int i = 0, j = n2 - 1; i < n2; i++, j--) b[i] = s2[j] - '0';fer(i, 0, max(n1, n2)){c[i] += a[i] + b[i];c[i + 1] += c[i] / 10;c[i] %= 10;}int len = max(n1, n2) + (c[max(n1, n2)] != 0);ferd(i, len - 1, 0) res += c[i] + '0';return res;
}signed main() {ios::sync_with_stdio(false); cin.tie(nullptr);string s;string ans = "";while(cin >> s, s != "0"){ans = add(ans, s);}cout << ans << '\n';return 0;
}
C.Word Amalgamation
题面
思路
题意是说先输入一组词典,词典中都是正确的单词,然后输入一组乱序的词,这些乱序的词可以经过重新将字母排序得到词典中的单词(或者词典中不存在)。如果存在就依次输出乱序单词对应的正确单词,不存在则另输出。
思路:我们只需要比较乱序单词和词典中的单词中的各字母的频次即可。
示例代码
#include<bits/stdc++.h>using namespace std;#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << '\n'const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;signed main() {ios::sync_with_stdio(false); cin.tie(nullptr);string s;map<string, map<int, int>> mp1;map<string, map<int, int>> mp2;while(cin >> s, s != "XXXXXX"){for(auto c : s) mp1[s][c - 'a']++;}vector<string> arr;while(cin >> s, s != "XXXXXX"){arr.push_back(s);for(auto c : s) mp2[s][c - 'a']++;}for(auto x : arr){bool ok = false;for(auto [y, mp] : mp1){if(mp1[y] == mp2[x]){ok = true;cout << y << '\n';}}if(!ok) cout << "NOT A VALID WORD\n";cout << "******\n";}return 0;
}