简单模拟题。

思路:由于数据量太大,直接用字符串去模拟是不行的。可以直接通过字母出现的次数通过迭代计算第N次字母出现的次数。注意:Please output a blank line after each test case. 在每一个样例后面换行符。

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;


const int SIZE = 101;
__int64 hash[51][26];
char sz1[SIZE], sz2[SIZE];

void init(char *sz1, char *sz2)
{
    int len1 = strlen(sz1), len2 = strlen(sz2);
    for(int i = 0; i < len1; i++)
    {
        hash[0][sz1[i]-'a']++;
    }
    for(int i = 0; i < len2; i++)
    {
        hash[1][sz2[i]-'a']++;
    }
    return ;
}


int main()
{
    int T, n;
    int i, j;
    scanf("%d", &T);
    while(T--)
    {
        memset(hash, 0sizeof(hash));
        scanf("%s%s%d", sz1, sz2, &n);
        init(sz1, sz2);
        if(n == 0)
        {
            for(i = 0; i < 26; i++)
            {
                printf("%c:%I64d\n"'a'+i, hash[0][i]);
            }
        }
        else if(n == 1)
        {
            for(i = 0; i < 26; i++)
            {
                printf("%c:%I64d\n"'a'+i, hash[1][i]);
            }
        }
        else
        {
            for(i = 2; i <= n; i++)
            {
                for(j = 0; j < 26; j++)
                {
                    hash[i][j] = hash[i-2][j]+hash[i-1][j];
                }
            }
            for(i = 0; i < 26; i++)
            {
                printf("%c:%I64d\n"'a'+i, hash[n][i]);
            }
        }
        printf("\n");      //别忘了 
    }
    return 0;

} 

posted on 2012-08-23 11:16  有间博客  阅读(312)  评论(0编辑  收藏  举报