HDU 1708 Fibonacci String

http://acm.hdu.edu.cn/showproblem.php?pid=1708

不能直接用string加,最后的位数很多,会超内存。一定注意特殊处理k=0和k=1的情况。最后有空行

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int a[100],b[100],c[100];
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int k;
        string s1,s2;
        cin >> s1 >> s2 >> k ;
        map <char,int> M1;
        map <char,int> M2;
        map <char,int> M3;
        if(k==0)
        {
            for(int i=0;i<s1.length();i++)
                M1[s1[i]]++;
            for(int i=97;i<123;i++)
                printf("%c:%d\n",i,M1[i]);
        }
        else if(k==1)
        {
            for(int i=0;i<s2.length();i++)
                M2[s2[i]]++;
            for(int i=97;i<123;i++)
                printf("%c:%d\n",i,M2[i]);
        }
        else
        {
            for(int i=0;i<s1.length();i++)
                M1[s1[i]]++;
            for(int i=0;i<s2.length();i++)
                M2[s2[i]]++;
            while(--k)
                for(int i=97;i<123;i++)
                {
                    M3[i]=M1[i]+M2[i];
                    M1[i]=M2[i];
                    M2[i]=M3[i];
                }
            for(int i=97;i<123;i++)
                printf("%c:%d\n",i,M3[i]);
        }
        putchar('\n');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiaohongmao/p/2517406.html