zoukankan      html  css  js  c++  java
  • hihocoder 1680 hiho字符串2 dp求方案数+递归

    我们定义第一代hiho字符串是"hiho"。  

    第N代hiho字符串是由第N-1代hiho字符串变化得到,规则是:

    h -> hio  

    i -> hi  

    o -> ho  

    例如第二代hiho字符串是: hiohihioho  

    第三代是: hiohihohiohihiohihohioho  

    给定N和K,请你计算第N代hiho字符串中的第K个字符是什么。

    Input

    第一行包含一个整数T,代表测试数据的组数。 (1 ≤ T ≤ 10)  

    以下T行每行包含两个整数N和K。  

    对于50%的数据,每一组的N都满足 1 ≤ N ≤ 15  

    对于100%的数据, 1 ≤ N ≤ 100, 1 ≤ K ≤ 1000000000

    Output

    对于每组数据,输出一行,包含一个字符代表答案。

    Sample Input

    3  
    1 1  
    2 5  
    3 7

    Sample Output

    h  
    i  
    o





    大意:初始字符串为hiho,一轮过后,h变为hio,i变成hi,o变成ho
    问N轮过后字符串中第K个字符是什么。


    题解:f[j][i]代表第 j 种字符经过 i-1 轮变换后变成了多少字符。(j==1代表 h ,j==2 代表 i ,j==3代表 o)
    然后划分第N轮的字符,确定第K个在哪部分,然后进入第N-1轮,继续划分……很明显的递归性质。
     1 /*
     2 Welcome Hacking
     3 Wish You High Rating
     4 */
     5 #include<iostream>
     6 #include<cstdio>
     7 #include<cstring>
     8 #include<ctime>
     9 #include<cstdlib>
    10 #include<algorithm>
    11 #include<cmath>
    12 #include<string>
    13 using namespace std;
    14 int read(){
    15     int xx=0,ff=1;char ch=getchar();
    16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
    18     return xx*ff;
    19 }
    20 const int limit=1000000000;
    21 int T,N,K;
    22 long long f[4][110];//h:1  i:2  o:3
    23 void dfs(int arg,int depth,int sum){
    24     if(depth==1){
    25         if(arg==1)
    26             printf("h
    ");
    27         else if(arg==2)
    28             printf("i
    ");
    29         else
    30             printf("o
    ");
    31         return;
    32     }
    33     if(arg==1){
    34         if(sum<=f[1][depth-1])
    35             dfs(1,depth-1,sum);
    36         else{
    37             sum-=f[1][depth-1];
    38             if(sum<=f[2][depth-1])
    39                 dfs(2,depth-1,sum);
    40             else{
    41                 sum-=f[2][depth-1];
    42                 dfs(3,depth-1,sum);
    43             }
    44         }
    45     }
    46     else if(arg==2){
    47         if(sum<=f[1][depth-1])
    48             dfs(1,depth-1,sum);
    49         else{
    50             sum-=f[1][depth-1];
    51             dfs(2,depth-1,sum);
    52         }
    53     }
    54     else{
    55         if(sum<=f[1][depth-1])
    56             dfs(1,depth-1,sum);
    57         else{
    58             sum-=f[1][depth-1];
    59             dfs(3,depth-1,sum);
    60         }
    61     }
    62 }
    63 int main(){
    64     //freopen("in","r",stdin);
    65     //freopen("out","w",stdout);
    66     for(int i=1;i<=3;i++)
    67         f[i][1]=1;
    68     for(int i=2;i<=100;i++){
    69         f[1][i]=f[1][i-1]+f[2][i-1]+f[3][i-1];
    70         f[2][i]=f[1][i-1]+f[2][i-1];
    71         f[3][i]=f[1][i-1]+f[3][i-1];
    72         for(int j=1;j<=3;j++)
    73             if(f[j][i]>limit)
    74                 f[j][i]=limit+1;
    75     }
    76     T=read();
    77     while(T--){
    78         N=read(),K=read();
    79         if(K<=f[1][N])
    80             dfs(1,N,K);
    81         else{
    82             K-=f[1][N];
    83             if(K<=f[2][N])
    84                 dfs(2,N,K);
    85             else{
    86                 K-=f[2][N];
    87                 if(K<=f[1][N])
    88                     dfs(1,N,K);
    89                 else{
    90                     K-=f[1][N];
    91                     dfs(3,N,K);
    92                 }
    93             }
    94         }
    95     }
    96     return 0;
    97 }
    View Code





  • 相关阅读:
    [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
    HDU4607(求树中的最长链)
    java从文件中读取数据然后插入到数据库表中
    rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系
    HDU 2084 数塔
    Object-c学习之路二(oc内存管理黄金法则1)
    android adb命令 unable to connect to 192.168.1.155:5555
    安装node.js / npm / express / KMC
    oracle断电重启之ORA-00600[4194]
    virtualbox虚拟机迁移出现"connot find device eth0"错误
  • 原文地址:https://www.cnblogs.com/lzhAFO/p/8253648.html
Copyright © 2011-2022 走看看