题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner in = new Scanner(System.in); String inString = in.nextLine(); //String inString = "aade * $afaefeafeafwafaefeafawf* fafaf"; String result = calc(inString); System.out.println(result); } public static String calc(String inString){ StringBuffer sb = new StringBuffer(inString); StringBuffer temp = new StringBuffer(); int count = 0; for(int i=0;i<sb.length();i++){ if(count == 20){ count = 0; temp.append(" "); } char c = sb.charAt(i); if( ( 'A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ){ temp.append(c); count++; continue; } if(temp.length() == 0){ continue; } if(temp.charAt(temp.length()-1) == ' '){ continue; } temp.append(" "); count = 0; } String[] res = temp.toString().split(" "); StringBuffer result = new StringBuffer(); for(int i= res.length-1;i > -1;i--){ result.append(res[i]).append(" "); } return result.substring(0,result.length()-1); }
}