标签:题目 距离 treeset print 字符串 problem res ima val
import java.util.Scanner; import java.util.TreeSet; public class Main { public static String go( String a, String b, String c ) { TreeSet<String> set = new TreeSet<String>(); if( !a.contains( b ) || !a.contains( c ) ) { return "No"; } else { int dist = Integer.MAX_VALUE; for( int i = 0; i < a.length(); i++ ) { int index1 = a.indexOf( b, i ); int index2 = a.indexOf( c, i ); if( index1 >= 0 && index2 >= 0 ) { if( Math.abs( index1 - index2 ) <= dist ) { int start = 0; int end = 0; if( index2 > index1 ) { end = index2 + c.length(); start = index1; } else { end = index1 + b.length(); start = index2; } dist = Math.abs( index1 - index2 ); String result = a.substring( start, end ); if(set.size() > 0){ String pre = set.first(); if(result.length() < pre.length()){ set.pollFirst(); } } set.add( result ); } } } } return set.first(); } public static void main( String[] args ) { Scanner sc = new Scanner( System.in ); if( sc.hasNext() ) { int num = sc.nextInt(); for( int i = 0; i < num; i++ ) { String a = sc.next(); String b = sc.next(); String c = sc.next(); System.out.println( go( a, b, c ) ); } } } }
标签:题目 距离 treeset print 字符串 problem res ima val
原文地址:http://www.cnblogs.com/dick159/p/6268806.html