Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week02/i/Passwords.java

/*
 *  Author: Son Pham
 *    Date: January 23, 2013
 * Purpose: Solves this problem using the simplest, but most inefficient approach.
 * Problem: 628 - Passwords
 */

import java.util.Arrays;
import java.util.Scanner;

public class Passwords {
    static String word;
    static int[] digits = new int[7];
    
    public static void main(String[] args) {
      
        int n,m;
        String[] words = new String[100];
        String[] rules = new String[1000];
        
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            n = Integer.parseInt((scan.next()));
            for(int i = 0; i < n; i++){
                words[i] = scan.next();
            }
            m = Integer.parseInt((scan.next()));
            for(int i = 0; i < m; i++){
                rules[i] = scan.next();
            }
            System.out.println("--");
            for(int i = 0; i < m; i++){
                String rule = rules[i];
                Arrays.fill(digits,-1);
                int index = 0;
                int count = 0;
                while((index = rule.indexOf("0",index)) != -1){
                    digits[count++] = index++;
                }
                
                for(int j = 0; j < n; j++){
                    word = words[j];
                    char[] rChars = rule.toCharArray();
                    digits(rChars,0);
                }
            }
        }   
    }
    public static void digits(char[] ch, int d){
        int spot = d;
        for(int i = 0; i < 10; ++i){
            String op = "";
            ch[digits[spot]] = Character.forDigit(i,10);
            if(d < ch.length){
                if(digits[++d] != -1){
                    digits(ch,d);
                    ch[digits[d]] = Character.forDigit(0,10);
                    d = spot;
                    continue;
                }
            }
            for(char c : ch){
                op+=c;
            }
            op = op.replaceAll("#",word);
            System.out.println(op);
            
        }
    }
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015