1sting
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5129 Accepted Submission(s): 1921
Problem Description
You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.
Input
The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.
Output
The output contain n lines, each line output the number of result you can get .
Sample Input
3
1
11
11111
Sample Output
1
2
8
————————————————————————————
//思路:
- 首先考虑到是一个和“超级阶梯”相似的问题。(每一个新的“l”都是前面一步走一步或者,前面两步走了两步)
- 所以是一个斐波那契数列。每一项是前两项的和。
- 再则因为最大是 maximum 200 ,易想到是一个大数问题。
- 所以要运用大数相加的方法,因为要先打表把所有的斐波拉契数给求出,所以设二维数组。
- 大数相加的时候注意进位。
————————————————————————————
#include<cstdio>
#include<cstring>
int main()
{
int t,len,flag;
char ch[205];
int sum[201][200],last[201][200];
int i,j;
memset(sum,0,sizeof(sum));// 数组清零
sum[1][0]=1;
sum[2][0]=2;
for( i=3;i<=200;++i) //斐波拉契数列从第三项开始
{
for(j=0;j<=199;++j) //位数的控制
{
sum[i][j]+=sum[i-1][j]+sum[i-2][j]; //把两数对应位相加
if(sum[i][j]>=10) //如果大于等于10,进位,下一位+1,本位-10
{
sum[i][j+1]+=1;
sum[i][j]-=10;
}
}
}
scanf("%d",&t);
while( t-- )
{
flag=1;
getchar();
scanf("%s",ch);
len=strlen(ch);
for( j=0; j<=199; j++)
//倒叙输出,因为数组在储存的时候从个位开始 个-十-百-
{
if( sum[len][199-j] !=0 || flag == 0)
{
printf("%d",sum[len][199-j]);
flag=0;//从第一个不是0的地方开始输出
}
}
printf("\n");
}
return 0;
}
字符串合并计数问题

本文介绍了一种基于斐波那契数列解决字符串合并计数问题的方法,通过使用大数相加的技术,解决了输入字符串最大长度为200的情况。文章提供了一个完整的C++实现示例。
8万+

被折叠的 条评论
为什么被折叠?



