您的位置:首页 > 其它

Hdu 1708 Fibonacci String

2012-08-23 11:16 351 查看
简单模拟题。

思路:由于数据量太大,直接用字符串去模拟是不行的。可以直接通过字母出现的次数通过迭代计算第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, 0, sizeof(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
[i]);
}
}
printf("\n"); //别忘了
}
return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: