Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:示例运行没错,submit结果Time Limit Exceeded,求大神解惑In Reply To:示例运行没错,submit结果Time Limit Exceeded,求大神解惑 Posted by:mouxiaochou at 2017-09-27 21:51:11 > import java.util.*; > > public class Main > { > public static void main(String[] args) > { > Scanner scan = new Scanner(System.in); > int num = scan.nextInt(); > int flag = 0; > int i; > ArrayList<String> strArray = new ArrayList<String>(); > ArrayList<String> strArray1 = new ArrayList<String>(); > String str = scan.nextLine(); > String s; > for(i = 1; i <= num; i++) { > str = scan.nextLine(); > s = change(str); > strArray.add(s); > } > while(!strArray.isEmpty()) { > String s1 = (String)strArray.get(0); > int count = 0; > Iterator<String> iter = strArray.iterator(); > String element; > while(iter.hasNext()) { > element = iter.next(); > if(s1.equals(element)) { > count++; > iter.remove(); > } > } > if(count > 1) { > StringBuilder sb = new StringBuilder(s1); > sb.append(" "+count); > strArray1.add(sb.toString()); > } > else > flag++; > } > if(flag == num) > System.out.println("No duplicates."); > else { > Collections.sort(strArray1); > for(String element : strArray1) > System.out.println(element); > } > scan.close(); > } > > public static String change(String str) { > str = str.replace("-", ""); > for(int i = 0; i < str.length(); i++) { > if(str.charAt(i) == 'A' || str.charAt(i) == 'B' || str.charAt(i) == 'C') > str = str.replace(str.charAt(i), '2'); > if(str.charAt(i) == 'D' || str.charAt(i) == 'E' || str.charAt(i) == 'F') > str = str.replace(str.charAt(i), '3'); > if(str.charAt(i) == 'G' || str.charAt(i) == 'H' || str.charAt(i) == 'I') > str = str.replace(str.charAt(i), '4'); > if(str.charAt(i) == 'J' || str.charAt(i) == 'K' || str.charAt(i) == 'L') > str = str.replace(str.charAt(i), '5'); > if(str.charAt(i) == 'M' || str.charAt(i) == 'N' || str.charAt(i) == 'O') > str = str.replace(str.charAt(i), '6'); > if(str.charAt(i) == 'P' || str.charAt(i) == 'R' || str.charAt(i) == 'S') > str = str.replace(str.charAt(i), '7'); > if(str.charAt(i) == 'T' || str.charAt(i) == 'U' || str.charAt(i) == 'V') > str = str.replace(str.charAt(i), '8'); > if(str.charAt(i) == 'W' || str.charAt(i) == 'X' || str.charAt(i) == 'Y') > str = str.replace(str.charAt(i), '9'); > } > StringBuilder sb = new StringBuilder(str); > sb.insert(3, "-"); > return sb.toString(); > } > } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator