试题描述
问题详情如下图所示: 要求:java实现
、数据结构知识
参考代码
比较垃圾,在别人基础上改的,懒得重构,,,仅供参考
import java. util. Scanner ; public class HDU1020StringEncoding { public int row; public char [ ] [ ] data; public char [ ] rowData = new char [ 20000 ] ; public HDU1020StringEncoding ( int row) { this . row = row; this . data = new char [ row] [ 10000 ] ; } public void loading ( ) { for ( int i = 0 ; i < this . row; i++ ) { Scanner scanner = new Scanner ( System . in) ; String rowStr = scanner. nextLine ( ) ; int length = rowStr. length ( ) ; for ( int j = 0 ; j < length; j++ ) { char c = rowStr. charAt ( j) ; this . data[ i] [ j] = c; } } } public void computed ( ) { for ( int i = 0 ; i < this . row; i++ ) { char [ ] chars = this . data[ i] ; for ( int j = 0 ; j < chars. length; j++ ) { char c = chars[ j] ; if ( c == '\0' ) { break ; } if ( checkHave ( c, j, chars) ) { continue ; } int number = computedNumber ( c, chars) ; setRowData ( number, c) ; } for ( char rowDatum : this . rowData) { if ( rowDatum == '\0' ) { break ; } System . out. print ( rowDatum) ; } System . out. print ( "\n" ) ; this . rowData = new char [ 20000 ] ; } } public boolean checkHave ( char c, int index, char [ ] arr) { for ( int i = 0 ; i < index; i++ ) { if ( arr[ i] == c) { return true ; } } return false ; } public int computedNumber ( char c, char [ ] ar) { int size = 0 ; for ( char c1 : ar) { if ( c1 == c) { size++ ; } if ( c1 == '\0' ) { break ; } } return size; } public void setRowData ( int number, char c) { if ( number == 0 ) { return ; } if ( number == 1 ) { for ( int i = 0 ; ; i++ ) { if ( this . rowData[ i] != '\0' ) { continue ; } this . rowData[ i] = c; break ; } } if ( number> 1 ) { for ( int i = 0 ; ; i++ ) { if ( this . rowData[ i] != '\0' ) { continue ; } String str = String . valueOf ( number) ; int len = str. length ( ) ; for ( int j= i; j- i< len; j++ ) { this . rowData[ j] = str. charAt ( j- i) ; } this . rowData[ i+ len] = c; break ; } } } public static void main ( String [ ] args) { Scanner scanner = new Scanner ( System . in) ; int row = scanner. nextInt ( ) ; HDU1020StringEncoding hduse = new HDU1020StringEncoding ( row) ; hduse. loading ( ) ; hduse. computed ( ) ; }
}
测试效果