首页 > 专题系列 > Java解POJ > POJ 3049 Securing the Barn [解题报告] Java
2013
11-12

POJ 3049 Securing the Barn [解题报告] Java

Securing the Barn

问题描述 :

Farmer John has installed a new security system on the barn and now must issue a valid password to the cows in the herd. A valid password consists of L (3 <= L <= 15) different lower-case characters (from the traditional latin character set 'a'...'z'), has at least one vowel ('a', 'e', 'i', 'o', or 'u'), at least two consonants (non-vowels), and has characters that appear in alphabetical order (i.e., 'abc' is valid; 'bac' is not).

Given a desired length L along with C lower-case characters, write a program to print all the valid passwords of length L that can be formed from those letters. The passwords must be printed in alphabetical order, one per line.

输入:

* Line 1: Two space-separated integers, L and C

* Line 2: C space-separated lower-case characters that are the set of characters from which to build the passwords

输出:

* Lines 1..?: Each output line contains a word of length L characters (and no spaces). The output lines must appear in alphabetical order.

样例输入:

4 6
a t c i s w

样例输出:

acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw

温馨提示:

INPUT DETAILS:

Passwords of length 4 chosen from the given six characters

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
	
	static final int N = 20;
	static int n,l;
	static String ans;
	static char word[] = new char[N];
	static char ansp[] = new char[N];
	
	public static void main(String []args)throws Exception{
		
		int i;
		String str;
		//Scanner cin = new Scanner(new FileInputStream("input.txt"));
		Scanner cin = new Scanner(System.in);
		l = cin.nextInt();
		n = cin.nextInt();
		for(i=0;i< n;++i){
			str = cin.next();
			word[i] = str.charAt(0);
		}
		Arrays.sort(word,0,n);
		dfs(0,0,false);
	}

public static void dfs(int start,int dept,boolean have){
	int i;
	if(dept>=l){
		if(!have) return ;
		for(i=0;i< dept;++i)
			System.out.print(ansp[i]);
		System.out.println("");
		return ;
	}
	for(i=start;i< n;++i){
	  ansp[dept] = word[i];
         if(word[i]=='a' || word[i]=='e'||word[i]=='i'||word[i]=='o'||word[i]=='u'){
		dfs(i+1,dept+1,have|true);
	   }
		else dfs(i+1,dept+1,have);
	}
 }
}

  1. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }