1sting

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7573    Accepted Submission(s): 2945

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


Sample Output


题目大意

给你一个仅包含'1'的字符串; 你可以将两个相邻的“1”合并为“2”,或将“1”保留。例如,给定1111,可以获得1111,121,112,211,22。 要求计算出可能的结果数量。

题目分析

当只有一个‘1’的时候,只有一种情况

  有两个‘1’的时候,有两种情况,一种是 11 一种是 2

  n>2的时候,我们可以在n-1的基础上在字符串的末尾加上一个 1

          也可以在n-2的基础上加上一个2

所以得出递推公式:
  f(n) = f(n-1) + f (n-2)

代码

#include<bits/stdc++.h>

using namespace std;

int n,i;
string x;
string bigadd(string a,string b)
{
int jin=,i;
char ai,bi;
string anss=a;
int lena=a.size();
int lenb=b.size();
int lenmax=max(lena,lenb);
int p=lena-;
int q=lenb-;
for(i=lenmax-;i>=;i--)
{
if(p<)
ai='';
else
ai=a[p];
if(q<)
bi='';
else
bi=b[q];
anss[i]=((ai-''+bi-''+jin)%)+'';
jin=(ai-''+bi-''+jin)/;
p--;
q--;
}
if(jin)
{
char x=jin+'';
anss=x+anss;
}
return anss;
}
int main()
{
string a[];
a[]="";
a[]="";
for(i=;i<;++i)
a[i]=bigadd(a[i-],a[i-]);
scanf("%d",&n);
for(i=;i<=n;i++)
{
cin>>x;
cout<<a[x.size()]<<endl;
}
return ;
}

HDU 1865 1sting (递推、大数)的更多相关文章

  1. 【hdoj_1865】1sting(递推+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1865 本题的关键是找递推关系式,由题目,可知前几个序列的结果,序列长度为n=1,2,3,4,5的结果分别是 ...

  2. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  3. HDOJ/HDU 1865 1sting(斐波拉契+大数~)

    Problem Description You will be given a string which only contains '1'; You can merge two adjacent ' ...

  4. Tiling(递推+大数)

    Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tili ...

  5. HDU 4747 Mex 递推/线段树

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...

  6. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  7. 致初学者(四):HDU 2044~2050 递推专项习题解

    所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...

  8. HDU 2604 Queuing(递推+矩阵)

    Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...

  9. HDU - 2604 Queuing(递推式+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  10. hdu 1723 DP/递推

    题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...

随机推荐

  1. qt5--鼠标操作

    #include "mylabel.h" #include <QDebug> #include <QPointF> #include <QPoint& ...

  2. vuex , 简单入(liao)门(jie)

    vuex什么 ? 官方的说法就是 vuex是专门为vue.js应用程序开发的 状态管理模式 .并采用集中式存储 , 管理应用的所有组件的状态 ,并以相同的规则保证状态以一种可预估的方式发生变化. 自己 ...

  3. sql 导入文件

    zai SQLQuery4.sql 文件中 --BULK INSERT Table_1 from 'D:\aaaa#azzz.txt' with(fieldterminator=',',rowterm ...

  4. java web文件上传功能实现

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  5. 「CQOI2014」数三角形

    题目链接 问题分析 可以先任意选\(3\)个数,然后减去三点共线的部分. 三点共线又分\(2\)种情况: 横的或者竖的.这一部分方案数是\(n\times{m\choose 3}+m\times {n ...

  6. Spring boot之返回json数据

    1.步骤: 1. 编写实体类Demo 2. 编写getDemo()方法 3. 测试 2.项目构建 编写实体类Demo package com.kfit; /** * 这是一个测试实体类. */ pub ...

  7. nginx 与location语法详解

    Location语法优先级排列   匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 ...

  8. python3笔记六:for语句

    一:学习内容 for语句 二:for-in语句 1. 格式 for 变量名 in 集合:    语句 2.逻辑 按顺序取集合中的每个元素赋值给变量,再去执行语句,如此循环往复 3.举例 for i i ...

  9. xshell上windows和linux互传文件命令

    1.安装lrzsz包: yum install -y lrzsz 2.从windows上传文件到linux服务器: rz 会弹出选择文件窗口,按照提示做就行3.从linux服务器下载文件到本地的win ...

  10. 构建基于Electron开发的软件遇到的问题

    构建pdman时,报了好些错. 主要还是网络问题和版本不一致导致的. 前提 npm设置淘宝源,自行搜索. 版本 上面是官方要求的node环境. 需要首先安装nvm, brew install nvm ...