USACO_3_2_Stringsobits

这道题不错,NOCOW上面已经有很详细的解答,就不再废话多了。

提示:通过集合的大小来求集合中第i大的元素

 

ContractedBlock.gifExpandedBlockStart.gifCode
ExpandedBlockStart.gifContractedBlock.gif/**//*
ID: sdjllyh1
PROG: kimbits
LANG: JAVA
complete date: 2009/1/21
complexity: O(S * L)
author: LiuYongHui From GuiZhou University Of China
more articles: www.cnblogs.com/sdjls
*/


import java.io.*;
import java.util.*;

public class kimbits
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static int S, L;
    
private static long I;
    
private static StringBuffer answer = new StringBuffer();

    
public static void main(String[] args) throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        init();
        run();
        output();
        System.exit(
0);
    }


    
private static void run()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        print(S, L, I);
    }

    
//递归输出长度为s,最多有l个1的集合中第i个串
    private static void print(int s, int l, long i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (s == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (i == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                answer.append(
'0');
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                answer.append(
'1');
            }

        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (i <= Size(s - 1, l))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                answer.append(
'0');
                print(s 
- 1, l, i);
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                answer.append(
'1');
                print(s 
- 1, l - 1, i - Size(s - 1, l));
            }

        }

    }


    
private static long[][] size;//size[s][l]表示长度为s,最多有l个1的集合大小
    private static long unknownSize = -1;//size未知时的值
    
//获得长度为s,最多有l个1的集合大小
    private static long Size(int s, int l)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (size[s][l] != unknownSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return size[s][l];
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            size[s][l] 
= Size(s - 1, l) + Size(s - 1, l - 1);
            
return size[s][l];
        }

    }


    
private static void init() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        BufferedReader f 
= new BufferedReader(new FileReader("kimbits.in"));
        StringTokenizer st 
= new StringTokenizer(f.readLine());
        S 
= Integer.parseInt(st.nextToken());
        L 
= Integer.parseInt(st.nextToken());
        I 
= Long.parseLong(st.nextToken());
        f.close();
        size 
= new long[S+1][L + 1];
        
for (int i = 1; i <= S; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Arrays.fill(size[i], unknownSize);
        }

        Arrays.fill(size[
1], 2);
        
for (int i = 1; i <= S; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            size[i][
0= 1;
        }

    }


    
private static void output() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("kimbits.out")));
        out.println(answer);
        out.close();
    }

}

转载于:https://www.cnblogs.com/SDJL/archive/2009/01/21/1379513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值