//Subsequences.java // 2^N of them. aka powerset import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Subsequences extends JApplet { JTextField inputStringField; JTextArea listArea; JLabel errorLabel; java.util.List subsAlphaDupsList; java.util.List subsSizeDupsList; TreeSet subsAlphaUniqSet; java.util.List subsSizeUniqList; JRadioButton radioAlphaButton, radioSizeButton; ButtonGroup buttonGroup; JRadioButton radioUniqueButton, radioDupsButton; ButtonGroup buttonGroup2; RadioButtonHandler radioButtonHandler; public void init() { Container container = getContentPane(); int panelRowsSize = 2; container.setLayout( new GridLayout( 1, panelRowsSize) ); JPanel [] panelRows = new JPanel[panelRowsSize]; //left side panel panelRows[0] = new JPanel(); container.add( panelRows[0] ); panelRows[0].setLayout( new GridLayout( 2, 1 ) ); JPanel topleftPanel = new JPanel(); panelRows[0].add( topleftPanel ); JPanel bottomleftPanel = new JPanel(); panelRows[0].add( bottomleftPanel ); bottomleftPanel.setLayout( new GridLayout( 1, 2 ) ); JPanel bottomleftPanelleft = new JPanel(); bottomleftPanel.add( bottomleftPanelleft ); bottomleftPanelleft.setLayout( new GridLayout( 2, 1 ) ); JPanel bottomleftPanelright = new JPanel(); bottomleftPanel.add( bottomleftPanelright ); bottomleftPanelright.setLayout( new GridLayout( 2, 1 ) ); //right side panel panelRows[1] = new JPanel(); panelRows[1].setLayout( new FlowLayout() ); container.add( panelRows[1] ); subsAlphaDupsList = new ArrayList(); Font bigFont = new Font( "Monospaced", Font.BOLD, 16 ); TextFieldHandler handleTextField = new TextFieldHandler(); JLabel inputNLabel = new JLabel( "String" ); inputNLabel.setFont( bigFont ); topleftPanel.add( inputNLabel ); inputStringField = new JTextField( 30 ); inputStringField.setFont( bigFont ); topleftPanel.add( inputStringField ); inputStringField.addActionListener( handleTextField ); errorLabel = new JLabel( "" ); topleftPanel.add( errorLabel ); radioAlphaButton = new JRadioButton( "Alphabetic order" ); bottomleftPanelleft.add( radioAlphaButton ); radioButtonHandler = new RadioButtonHandler(); radioAlphaButton.addItemListener( radioButtonHandler ); radioSizeButton = new JRadioButton( "By size order" ); bottomleftPanelleft.add( radioSizeButton ); radioSizeButton.addItemListener( radioButtonHandler ); //radioBinaryButton = new JRadioButton( "By binary number order" ); //bottomleftPanelleft.add( radioBinaryButton ); //radioBinaryButton.addItemListener( radioButtonHandler ); buttonGroup = new ButtonGroup(); buttonGroup.add(radioAlphaButton); buttonGroup.add(radioSizeButton); //buttonGroup.add(radioBinaryButton); radioUniqueButton = new JRadioButton( "Uniques only" ); bottomleftPanelright.add( radioUniqueButton ); radioUniqueButton.addItemListener( radioButtonHandler ); radioDupsButton = new JRadioButton( "Dups OK" ); bottomleftPanelright.add( radioDupsButton ); radioDupsButton.addItemListener( radioButtonHandler ); buttonGroup2 = new ButtonGroup(); buttonGroup2.add(radioUniqueButton); buttonGroup2.add(radioDupsButton); listArea = new JTextArea( 16, 30 ); Font smallFont = new Font( "Monospaced", Font.BOLD, 12 ); listArea.setFont( smallFont ); JScrollPane listScroll = new JScrollPane( listArea ); panelRows[1].add( listScroll ); } public void start() { inputStringField.requestFocusInWindow(); radioAlphaButton.setSelected( true ); //check in handler if start() radioDupsButton.setSelected( true ); } private class TextFieldHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { listArea.setText( "" ); String s = inputStringField.getText(); int n = s.length(); errorLabel.setText( "" ); if ( n > 31 ) { errorLabel.setText( "up to 31 chars only" ); return; } char[] A=s.toCharArray(); int two_n = 1 << n; subsAlphaDupsList.clear(); //generate in binary number order for (int i=0; i<=two_n-1; i++) { String subset = ""; for (int j=0; j>j) & 1) == 1) subset += "" + A[j]; subsAlphaDupsList.add( subset ); } //sort into alphabetic order Collections.sort( subsAlphaDupsList ); //alpha & dups subsSizeDupsList = new ArrayList( subsAlphaDupsList) ; //sort into length order (stable by alpha) Collections.sort( subsSizeDupsList, new Comparator() { public int compare (Object o1, Object o2) { return ((String)o1).length() - ((String)o2).length(); } } ); //size & dups //alphabetic and no dups subsAlphaUniqSet = new TreeSet( subsAlphaDupsList ); //alpha & unique //build ordered-by-sizes list. subsSizeUniqList = new ArrayList( subsAlphaUniqSet ); Collections.sort( subsSizeUniqList, new Comparator() { public int compare (Object o1, Object o2) { return ((String)o1).length() - ((String)o2).length(); } } ); //size & unique radioButtonHandler.displaySubstrings(); } } /* private void displaySizedSet() { listArea.setText( "" ); for (int i=0; i