【递推DP】HDU1865-1sting

来源:互联网 发布:标的物 知乎 编辑:程序博客网 时间:2024/05/21 09:08

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1865

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
311111111
 

Sample Output
128

递推DP代码:

#include<iostream>using namespace std;//  状态转移方程://  dp[i]=d[i-1]+dp[i-2];const int maxn=205;string dp[maxn]="";string add(string a,string b){    string ans="";    int aa[maxn]={0},bb[maxn]={0};    int aLen=a.size();    int bLen=b.size();    int maxLen=max(aLen,bLen);    for(int i=0;i<aLen;i++)        aa[aLen-i-1]=a[i]-'0';    for(int i=0;i<bLen;i++)        bb[bLen-i-1]=b[i]-'0';    for(int i=0;i<maxLen;i++){        aa[i]+=bb[i];        aa[i+1]+=aa[i]/10;        aa[i]%=10;    }    if(aa[maxLen]) maxLen++;    for(int i=maxLen-1;i>=0;i--)        ans+=aa[i]+'0';    return ans;}void faction(){    dp[0]="1";    dp[1]="1";    for(int i=2;i<=200;i++)        dp[i]=add(dp[i-1],dp[i-2]);}int main(){    int t,n;    string str;    cin.sync_with_stdio(false);    faction();    cin>>t;    while(t--){        cin>>str;        n=str.size();        cout<<dp[n]<<endl;    }    return 0;}


0 0
原创粉丝点击