so i revised it too many times.could someone let me know what is the problem?
this is my code
Code: Select all
import java.util.*;
import java.text.Collator;
class BruteForces {
public static int totalsum=0;
public static String outputAll1="";
public static String outputAll2="";
public static void main(String[] args) {
String[] InputDatasLoopAr1=new String[100];
String[] InputDatasLoopAr2=new String[100];
//iniatialize array;
for(int i = 0; i <100; i++) {
InputDatasLoopAr1[i] = new String();
InputDatasLoopAr2[i] = new String();
}
Scanner in = new Scanner(System.in);//use system.in to read input
int Intdatacase=0;
String datacase="";
//get data case
try { datacase = in.next();
Intdatacase=Integer.parseInt(datacase);
} catch ( Exception e) { System.exit(0); }
if ( datacase == null ) System.exit(0);
int intLoop=0,IntInputsz=0;
String InputDatas="";
while(intLoop<Intdatacase){
outputAll1="";
outputAll2="";
//get input for each case
InputDatas="";
String InputDatas2 ="";
//get how many words in a cases
try { IntInputsz = in.nextInt();
} catch ( Exception e) { break; }
//enter first words
try{
InputDatas = in.next();
} catch ( Exception e) { break; }
if ( InputDatas == null ) break;
for(int i=0;i<IntInputsz-1;i++)
{
String InputDatasLoop="";
//get next word if size word >1
try { InputDatasLoop = in.next(); } catch ( Exception e) {
break; }
if ( InputDatasLoop == null ) {break;}
if(InputDatasLoop.charAt(0)==InputDatas.charAt(0))
{
InputDatasLoopAr1[i]=InputDatasLoop;
}
else
InputDatasLoopAr2[i]=InputDatasLoop;
}
sortArray(InputDatasLoopAr1);//sort first array
sortArray(InputDatasLoopAr2);//sort second array
totalsum=InputDatas.length();//get first value of word
outputAll1=InputDatas;//insert first word
calculate(InputDatasLoopAr1,InputDatas,"A",totalsum);//calculate for first array
InputDatas="";//clear inputdatas
calculate(InputDatasLoopAr2,InputDatas,"D",totalsum);//calculate for second array
System.out.printf(totalsum+"\n"+outputAll1+"\n"+outputAll2);//display output
intLoop++;
}
System.exit(0);
}
public static void calculate(String[] strArray,String InputDatas,String Arrange,int total)
{ int LastIndex=InputDatas.length(),counts=1;
Boolean InputDatasB=false;
for(int iz=0;iz<100;iz++)
{
if(strArray[99].isEmpty())
{
break;
}
if((InputDatasB==true&&!strArray[iz].isEmpty())||(InputDatas.equals("")&&!strArray[iz].isEmpty()))
{
InputDatas=strArray[iz];
LastIndex=strArray[iz].length();
InputDatasB=false;
totalsum+=LastIndex;
}
int pos2 = strArray[iz].indexOf(InputDatas.substring(0, LastIndex));
if(pos2==0&&!strArray[iz].isEmpty())
{
if(Arrange.equals("D"))
outputAll2=strArray[iz]+"\n"+outputAll2;
else
outputAll1=outputAll1+"\n"+strArray[iz];
totalsum+=(strArray[iz].length()-LastIndex);
InputDatas=strArray[iz];
LastIndex=strArray[iz].length();
strArray[iz]="";
}
int CountNext=iz+1;
if(CountNext==strArray.length)
{
if(counts==1)
{
iz=0;
LastIndex--;
sortArray(strArray);
}
if(LastIndex==0)
{ sortArray(strArray);
InputDatasB=true;
return;
}
}
}
return;
}
public static void sortArray(String[] strArray){
String tmp;
Collator collator = Collator.getInstance(Locale.UK);
if (strArray.length == 1) return;
for (int i = 0; i < strArray.length;i++) {
for (int j = i + 1; j < strArray.length; j++) {
if( collator.compare(strArray[i], strArray[j] ) > 0 ) {
tmp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = tmp;
}
}
}
return;
}
}