import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.ArrayList; public class Compress { public static void main( String[] args ) throws IOException { // BufferedReader captures data from data file on disk drive BufferedReader in = new BufferedReader( new FileReader("Test.in" ) ); StringTokenizer st = null; String output = ""; String line = in.readLine(); // read new line of data while( line != null ) // detects end of file when line == null { st = new StringTokenizer( line ); while( st.hasMoreTokens() ) { placeWordInArrayList( st.nextToken() ); } // end while reduceListToUniqueWords( words ); output = getOutputFromList( words, line ); output = output.trim(); System.out.println( output ); line = in.readLine(); // read string from file words.clear(); } // end while } // end main( ) static String getOutputFromList( ArrayList list, String line ) { String word = "", tok = "", output = ""; int index = 0; char ch; int len; boolean wordBuilt = false; StringTokenizer st = new StringTokenizer( line ); while( st.hasMoreTokens() ) { tok = st.nextToken(); len = tok.length(); // loop through characters of tok for( int i = 0; i < len; i++ ) { ch = tok.charAt( i ); // grab next character // before letter is found, stay in loop // after letter is found, build word until no more letter found if( Character.isLetter( ch ) ) { wordBuilt = true; word = word + ch; // tests if token is a word and does not include nonletters. if( word.length() == len ) { index = findIndex( word, list ); if (index != -99) output = output + " " + ( index + 1 ); else output = output + " " + word ; word = ""; wordBuilt = false; } } else // token includes nonletters { if( wordBuilt == true ) // if a word was built before nonletter { index = findIndex( word, list ); if (index != -99) output = output + " " + ( index + 1 ); else output = output + " " + word ; } if( ch == '-' ) output = output + " -"; else output = output + ch; word = ""; wordBuilt = false; } // end else } } // end while return output; }// end getOutputFromList() static void reduceListToUniqueWords( ArrayList list ) { String word = ""; int len = list.size(); // Make every cell of words.token to be unique // cell words.count records number of instances in line for( int i = 0; i < len; i++ ) { word = list.get( i ).token; for( int j = i + 1; j < len; j++ ) { if( word.equals( list.get( j ).token ) ) { list.remove( j ); list.get( i ).count++; len--; if( j == len - 1 ) j--; } } // end for (int j ... ) } // end for (int i ... ) // remove all list.count == 1 cells for( int i = 0; i < len; i++ ) { if( list.get( i ).count < 2 ) { list.remove( i ); i--; len--; } } } // end reduceListToUniqueWords( ) static int findIndex( String word, ArrayList temp ) { int len = temp.size(), index = -99; for( int i = 0; i < len; i++ ) { if( word.equals( temp.get( i ).token ) ) index = i; } return index; } static void placeWordInArrayList( String tok ) { TokenRecord temp = new TokenRecord(); int len = tok.length(); char ch; String word = ""; boolean wordBuilt = false; // loop through characters of tok for( int i = 0; i < len; i++ ) { ch = tok.charAt( i ); // grab next character // before letter is found, stay in loop // after letter is found, build word until no more letter found if( Character.isLetter( ch ) ) { wordBuilt = true; word = word + ch; } else if( wordBuilt == true ) { break; } } // end for if( wordBuilt == true ) { temp.token = word; temp.count = 1; words.add( temp ); wordBuilt = false; } } // end placeTokenInAppropriateArrayList( ) static private class TokenRecord { public String token; public int count; } static private ArrayList words = new ArrayList(); } // end class Compress