Figure out the sequence
Figure out the sequence
https://ac.nowcoder.com/acm/contest/5523/F
Solution
如果单看这个递推式,可以很简单的发现除开第一项和第二项,从第三项开始都是自己加上前一项的结果(字符串拼接)作为自己的答案。因为n比较小,我选择模拟去算。那么就要设计到如何递推了,单纯变量肯定不行,因为涉及字母与次数的映射,选择用去循环模拟。
Code
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } map<char, ll> mp1, mp2, tmp; string a, b; int n; int main() { js; cin >> a >> b >> n; for (auto it : a) ++mp1[it]; if (n == 1) { //注意1,2特判 for (auto it : mp1) cout << it.first << ": " << it.second << endl; return 0; } for (auto it : b) ++mp2[it]; if (n == 2) { for (auto it : mp2) cout << it.first << ": " << it.second << endl; return 0; } n -= 2; while (n > 0) { tmp = mp2; --n; for (auto it : mp1) mp2[it.first] += mp1[it.first]; mp1 = tmp; } for (auto it : mp2) cout << it.first << ": " << it.second << endl; return 0; }