Hello if you solved this problem
http://code.google.com/codejam/contest/32016/dashboard#s=p1

can you review and correct me

==================START============================

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class Milkshakes {
 PrintWriter out = null;
 BufferedReader in = null;
 ArrayList<CustomerChoices> customerChoices = new
ArrayList<CustomerChoices>();
 int numberOfmc = 0;
 StringBuilder sb = new StringBuilder();
 String solutionString = "";
 int backtrackState=-1;

 public Milkshakes() {
  try {
   out = new PrintWriter(new FileWriter("C-small-practice.out"));
   in = new BufferedReader(new FileReader("B-small-practice.in"));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public String processSolutionString(String data) {
  String output[] = new String[numberOfmc];
  data = data.trim();
  String choices[] = data.split(" ");
  int len = choices.length;
  String aux[] = new String[2];
  int count = 0;
  for (int i = 0; i < len; i++) {
   aux = choices[i].split(":");
   output[Integer.parseInt(aux[0])-1] = aux[1];
  }
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < numberOfmc; i++) {
   if(output[i]==null){
    output[i]="0";
   }
   if(output[i].equals("1")){
    count++;
   }
   sb.append(output[i]).append(" ");
  }
  sb.append(":").append(count);
  return sb.toString();

 }

 public void doitIndp(StringAttr canterminate, String previousDecissions,
   int currentStage, int numberOfstages) {
  int max = 10000;
  CustomerChoices cc = customerChoices.get(currentStage);
  ArrayList<Choice> ccl = cc.getChoiceList();
  int size = ccl.size();
  Choice curentChoice=null;
  String decissionString = "";
  if (currentStage == numberOfstages-1) {
   // solution state

   for (int i = 0; i < size; i++) {
     curentChoice = ccl.get(i);
    decissionString = "";
    if (curentChoice.type == 1) {
     decissionString = " " + curentChoice.machineNumber + ":"
       + 0;
    } else {
     decissionString = " " + curentChoice.machineNumber + ":"
       + 1;
    }
    if (!previousDecissions.contains(decissionString)) {
     decissionString = " " + curentChoice.machineNumber + ":" +
curentChoice.type;
     String
solmalt[]=processSolutionString(previousDecissions+decissionString).split(":");
     int minMalted = Integer.parseInt(solmalt[1]);
     solutionString =solmalt[0];
     canterminate.setCanTerminate("YES");
   /*  if (minMalted <= max) {
      solutionString =solmalt[0];
      if (minMalted == 1) {
       canterminate.setCanTerminate("YES");
      }
     }*/
    }
   }
  } else {
   for (int i = 0; i < size; i++) {
    curentChoice = ccl.get(i);
    decissionString = "";
    if (curentChoice.type == 1) {
     decissionString = " " + curentChoice.machineNumber + ":"
       + 0;
    } else {
     decissionString = " " + curentChoice.machineNumber + ":"
       + 1;
    }
    if (!previousDecissions.contains(decissionString)) {
     decissionString = " " + curentChoice.machineNumber + ":"
       + curentChoice.type;
     doitIndp(canterminate,
       previousDecissions + decissionString,
       currentStage + 1, numberOfstages);
     if (canterminate.getCanTerminate() == "YES") {
      return;
     }
     if (canterminate.getCanTerminate() == "BT") {
      if(currentStage==backtrackState){
       backtrackState=0;
       canterminate.setCanTerminate("NO");
       continue;
      }else{
       return;
      }
     }
    }
   }
   if(currentStage!=0){
    int backstate=0;
    previousDecissions=previousDecissions.trim();
    String pst[]=previousDecissions.split(" ");

    for(;backstate<pst.length;backstate++){
     if(pst[backstate].trim().equals(decissionString.trim())){
      break;
     }
    }
    canterminate.setCanTerminate("BT");
    backtrackState=backstate;
   }
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  Milkshakes m = new Milkshakes();
  int stages = 0;
  try {
   int ntc = Integer.parseInt(m.in.readLine().trim());
   for (int i = 1; i <= ntc; i++) {
    m.customerChoices.clear();
    m.solutionString = "";
    m.numberOfmc = Integer.parseInt(m.in.readLine().trim());
    int malted[] = new int[m.numberOfmc];
    for (int l = 0; l < m.numberOfmc; l++) {
     malted[l] = -1;
    }
    stages = Integer.parseInt(m.in.readLine().trim());
    System.out.println("---------------------------------");
    System.out.println(m.numberOfmc + ":" + stages);
    System.out.println("---------------------------------");
    for (int j = 0; j < stages; j++) {
     m.customerChoices.add(new CustomerChoices(m.in.readLine()));
    }
    StringAttr sattr = new StringAttr("NO");
    m.doitIndp(sattr, "", 0,stages);

    if (m.solutionString == "") {
     m.out.println("Case #" + i + ": IMPOSSIBLE");
     System.out.println("Case #" + i + ": IMPOSSIBLE");
    } else {
     m.out.println("Case #" + i + ": " + m.solutionString.trim());
     System.out.println("Case #" + i + ": " + m.solutionString);
    }

   }
   m.out.close();
  } catch (NumberFormatException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

}

class Choice  {
 int machineNumber, type;

 public Choice(int machineNumber, int type) {
  this.machineNumber = machineNumber;
  this.type = type;
 }
}

class CustomerChoices {
 ArrayList<Choice> choiceList = new ArrayList<Choice>();

 public ArrayList<Choice> getChoiceList() {
  return choiceList;
 }

 public void setChoiceList(ArrayList<Choice> choiceList) {
  this.choiceList = choiceList;
 }

 public CustomerChoices(String choices) {
  String inputRequest = choices.trim();
  String d[] = inputRequest.trim().split(" ");
  int nc = Integer.parseInt(d[0]);
  for (int l = 1; l <= nc; l++) {
   int machineNumber = Integer.parseInt(d[2 * l - 1]);
   int type = Integer.parseInt(d[2 * l]);
   choiceList.add(new Choice(machineNumber, type));
  }
  Collections.sort(choiceList,new ChoiceComparator());
 }
}

class StringAttr {
 public StringAttr(String canTerminate) {
  this.canTerminate = canTerminate;
 }

 public String getCanTerminate() {
  return canTerminate;
 }

 public void setCanTerminate(String canTerminate) {
  this.canTerminate = canTerminate;
 }

 String canTerminate;
}
class ChoiceComparator implements Comparator<Choice>{

 @Override
 public int compare(Choice o1, Choice o2) {
  // TODO Auto-generated method stub
  if(o1.type>o2.type){
   return 1;
  }
  if(o1.type<o2.type){
   return -1;
  }
  return 0;
 }

}

===================STOP============================

On Sun, Mar 25, 2012 at 11:25 PM, Samuel Jawahar <[email protected]>wrote:

> Thanks a lot Ahmed
> Samuel
>
> On Sun, Mar 25, 2012 at 11:10 PM, Ahmed Aly <[email protected]>wrote:
>
>> $ is a special character, try the following code, and it should work:
>>
>> String aux="aewwewe$ssdfsdfdf$sdfsdfsdfs$rterter$";
>> System.out.println(aux.split("\\$").length);
>>
>> --
>> Ahmed Aly
>> www.ahmed-aly.com
>>
>> On Sun, Mar 25, 2012 at 10:34 AM, Samuel Jawahar <[email protected]>wrote:
>>
>>> code snippet:-
>>> String aux="aewwewe$ssdfsdfdf$sdfsdfsdfs$rterter$";
>>> System.out.println(aux.split("$").length);
>>> outPut:1
>>>
>>>
>>>
>>> On Wed, Mar 21, 2012 at 5:50 PM, mandy <[email protected]> wrote:
>>>
>>>> here is a link to the problem...
>>>> http://code.google.com/codejam/contest/351101/dashboard#s=p1
>>>> Input:-this is a test
>>>> Output:-test a is this
>>>> I'm getting error in output and in while loop is not executing
>>>> properly...
>>>> So pls tell me what's wrong with this code...
>>>> Thanks a lot.
>>>>
>>>> Mine solution is this..
>>>>
>>>> /*
>>>>  * To change this template, choose Tools | Templates
>>>>  * and open the template in the editor.
>>>>  */
>>>> package frame;
>>>>
>>>> import java.util.Scanner;
>>>>
>>>> /**
>>>>  *
>>>>  * @author Pardeep
>>>>  */
>>>> public class Reverse {
>>>>    public void test(){
>>>>        int n=0,l=0,i=0;
>>>>             int j=0,c=0;
>>>> Scanner sc=new Scanner(System.in);
>>>>        n=sc.nextInt();                            //count no of test
>>>> cases
>>>>        while(n>0){
>>>>            String strn="";
>>>>            String str=sc.nextLine();
>>>>             l=str.length();
>>>>             String[] ar=new String[l];
>>>>             j=0;
>>>>             int count=0;
>>>>             for(i=0;i<str.length();i++){         //count no of blank
>>>> spaces in string
>>>>                 if(str.charAt(i)==' '){
>>>>                     count++;
>>>>                 }
>>>>             }
>>>>             i=0;
>>>>            for(i=str.length()-1;i>=0;i--){      //reverse the string
>>>>             ar[j]=String.valueOf(str.charAt(i));  //store the value
>>>> in array ar[]
>>>>           //System.out.println("while.."+ar[j]+j);
>>>>                j+=1;                                 //then increase
>>>> value in array element
>>>>                if(str.charAt(i)==' '){              //if space occur
>>>> in string then pop up all array element and add in string variable
>>>> strn
>>>>                    for(int k=j-1;k>=0;k--){
>>>>                        strn+=ar[k];
>>>>                    }
>>>>                j=0;                                     // j is again
>>>> set to j=0
>>>>                count--;//System.out.println(count);     //count is
>>>> decremented
>>>>                }                                         //now we
>>>> have reversed the string and store it in string (strn),but not the
>>>> first word of string
>>>>                if(count==0){                               //if count
>>>> is zero then
>>>>                    strn+=" ";                              //space is
>>>> added to string
>>>>                    for(int m=0;m<=i;m++){                  //variable
>>>> m is initialised to 0 and upto i
>>>>                        strn+=str.charAt(m);                //store
>>>> each char in the string (strn)
>>>>                    }
>>>>                    i=-1;
>>>>                }
>>>>            }c++;
>>>>             System.out.println("Case #:"+c
>>>> +strn);                     //Error:-output is printed (but getting
>>>> space at first position)
>>>>
>>>>        n--;
>>>>        }
>>>>
>>>>    }
>>>>    public static void main(String a[]){
>>>>        new Reverse().test();
>>>>    }
>>>> }
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Google Code Jam" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/google-code?hl=en.
>>>>
>>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Google Code Jam" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-code?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Google Code Jam" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/google-code?hl=en.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to