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;
}
全部评论

相关推荐

27届毕业,最近想找一段大厂实习,感觉简历有些问题,好多都不给面,求大佬们指点,最近好焦虑
重生之我学Java干...:我从后端的角度分析一下你的第一个项目,我感觉亮点不是很突出。因为我是因为组内有需求,临时上手学react干活。我用到的技术基本就cover你那个智慧园区管理平台的很多亮点了。那作为比较专业的前端,你上述的内容是不是有点单薄呢。感觉还得包装
点赞 评论 收藏
分享
09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务