Hdu 1708 Fibonacci String

本文介绍了一种解决大规模数据模拟问题的方法,通过记录字母出现次数并使用迭代算法来高效计算特定次数下各字母的频率。文章提供了完整的C++代码实现,并强调了在输出每个测试用例后加入换行符的重要性。

简单模拟题。

思路:由于数据量太大,直接用字符串去模拟是不行的。可以直接通过字母出现的次数通过迭代计算第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;

} 

转载于:https://www.cnblogs.com/g0feng/archive/2012/08/23/2652113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值