POJ1002 java版本

来源:互联网 发布:c语言之家 编辑:程序博客网 时间:2024/05/21 17:46
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.*;class Main{    public static void main(String[] args) throws Exception{    //List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();     Map< String, Integer> tm = new TreeMap();    int cnt=0;    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    cnt = Integer.parseInt(in.readLine());    for(int i=0;i<cnt;i++){    String lin = "";    String str = in.readLine();    for(int j=0;j<str.length();j++){    if (Character.isDigit(str.charAt(j))) {                  lin+=str.charAt(j);                 continue;            }      char ch = str.charAt(j);    switch(ch){    case 'A':    case 'B':    case 'C':    lin+="2";    break;    case 'D':    case 'E':    case 'F':    lin+="3";    break;    case 'G':    case 'H':    case 'I':    lin+="4";    break;    case 'J':    case 'K':    case 'L':    lin+="5";    break;    case 'M':    case 'N':    case 'O':    lin+="6";    break;    case 'P':    case 'R':    case 'S':    lin+="7";    break;    case 'T':    case 'U':    case 'V':    lin+="8";    break;    case 'W':    case 'X':    case 'Y':    lin+="9";    break;    default:    break;    }        }       if (lin.length()==7) {    String newStr = lin.substring(0,3);        lin = newStr+"-"+lin.substring(3);}else {break;}        if (tm.containsKey(lin)) {               int count = tm.get(lin) + 1;                   tm.put(lin, count);               } else {                   tm.put(lin, 1);               }  }     Set se = tm.keySet();           Iterator it = se.iterator();           boolean flag = false;           while (it.hasNext()) {               String s = it.next().toString();               int count = tm.get(s);               if (count > 1) {                   flag = true;                   System.out.println(s + " " + count);               }           }         if (!flag) {               System.out.println("No duplicates. ");           }      }}

我是一直小小菜鸟,刚开始做poj上面的题,对一些错误类型还无法快速的辨别,因此刚开始的几道题花费了我非常多的时间可精力,尤其是这一道题。所以我再次与大家分享一下我的经验。刚开始肯定很多人都会发现runtime error;这个问题的原因有很多,大家可以在百度里面查到,介绍的很清楚,我就不多说了。然后就是wrong answer,这个要么就是你的算法有问题,导致结果不对,要么就是输出格式不对。这就需要我们非常仔细的去查看我的自己写的代码了。最后看着看着快成功了,没想到又除了个Presentation Error,哎呀,这个就非常坑爹,怎么办呢?又查呀。结果如下:“首先可以肯定的是,你的思路没有错,输出结果也与标准输出结果非!常!接!近!出现这个错误最可能的原因是,在输出结果的后面,多了或少了没什么意义的空格,tab,换行符等等”,对于我来说非常容易犯这种错误。最后看着没问题了,谁能想,算法除了问题导致超时,这就比较坑肉啦,这可是大改动,得重新设计算法来实现,我们都知道Java的读写速度比不上C/C++等,所以我们要更好的设计算法才能Accept。(最近弄这些个错误都魔症了,看见accpet就像刚开始学C的时候运行提示0 error一样激动),好吧,我让大家看看我刚开始设计的实现算法:先说明,我本来就是一直菜鸟,大神勿喷。只希望能够大家分享一下经历。好了,一下是刚开始的算法实现
import java.util.*;class Main{    public static void main(String[] args){    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();    int cnt=0;    Scanner sc = new Scanner(System.in);    cnt = Integer.parseInt(sc.nextLine());    for(int i=0;i<cnt;i++){    String lin = "";    String str = sc.nextLine();    for(int j=0;j<str.length();j++){    char ch = str.charAt(j);    switch(ch){    case 'A':    case 'B':    case 'C':    lin+="2";    break;    case 'D':    case 'E':    case 'F':    lin+="3";    break;    case 'G':    case 'H':    case 'I':    lin+="4";    break;    case 'J':    case 'K':    case 'L':    lin+="5";    break;    case 'M':    case 'N':    case 'O':    lin+="6";    break;    case 'P':    case 'R':    case 'S':    lin+="7";    break;    case 'T':    case 'U':    case 'V':    lin+="8";    break;    case 'W':    case 'X':    case 'Y':    lin+="9";    break;    case '1':    lin+="1";    break;    case '2':    lin+="2";    break;    case '3':    lin+="3";    break;    case '4':    lin+="4";    break;    case '5':    lin+="5";    break;    case '6':    lin+="6";    break;    case '7':    lin+="7";    break;    case '8':    lin+="8";    break;    case '9':    lin+="9";    break;    case '0':    lin+="0";    break;    default:    break;    }        }    if (lin.length()==7) {    String newStr = lin.substring(0,3);        lin = newStr+"-"+lin.substring(3);}else {break;}       if(list.size()==0){Map<String,Object> map = new HashMap<String,Object>();map.put("phone", lin);map.put("count", 1);list.add(map);}else{boolean isContains = false;   for(int k=0;k<list.size();k++){Map<String,Object> map = list.get(k);String getStr = map.get("phone").toString();    if(getStr.equals(lin)){    isContains = true;    int getCount = Integer.parseInt(map.get("count").toString());    map.put("count",getCount+1);    map.put("phone", getStr);    list.set(k, map);    }    }   if(!isContains){   Map<String,Object> map = new HashMap<String,Object>();   map.put("phone", lin);   map.put("count", 1);   list.add(map);   }}}    boolean isContains =false;    Sort(list);    for(int l=0;l<list.size();l++){    Map<String,Object> map = list.get(l);    int cnt1 = Integer.parseInt((map.get("count").toString()));    if(cnt1!=1){    isContains = true;    System.out.println(map.get("phone")+"    "+map.get("count"));    }    }if (!isContains) {System.out.println("No duplicates.");}    }private static void Sort(List<Map<String, Object>> list) {int length = list.size();for (int i = 0; i < length-1; i++) {for (int j = 0; j < length-i-1; j++) {String getPhone1 = (String) list.get(j).get("phone");String getPhone2 = (String) list.get(j+1).get("phone");if ((getPhone1.compareTo(getPhone2))>0) {int getCount1 = Integer.parseInt(list.get(j).get("count").toString());int getCount2 = Integer.parseInt(list.get(j+1).get("count").toString());list.get(j+1).put("phone", getPhone1);list.get(j+1).put("count", getCount1);list.get(j).put("phone", getPhone2);list.get(j).put("count", getCount2);}}}}}

写的比较繁琐,我自己也不满意,代码太多看着自己也很烦。提交的时候出现了超时错误,所以我重新设计的算法如下:

import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.*;class Main{    public static void main(String[] args) throws Exception{    //List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();     Map< String, Integer> tm = new TreeMap();    int cnt=0;    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    cnt = Integer.parseInt(in.readLine());    for(int i=0;i<cnt;i++){    String lin = "";    String str = in.readLine();    for(int j=0;j<str.length();j++){    if (Character.isDigit(str.charAt(j))) {                  lin+=str.charAt(j);                 continue;            }      char ch = str.charAt(j);    switch(ch){    case 'A':    case 'B':    case 'C':    lin+="2";    break;    case 'D':    case 'E':    case 'F':    lin+="3";    break;    case 'G':    case 'H':    case 'I':    lin+="4";    break;    case 'J':    case 'K':    case 'L':    lin+="5";    break;    case 'M':    case 'N':    case 'O':    lin+="6";    break;    case 'P':    case 'R':    case 'S':    lin+="7";    break;    case 'T':    case 'U':    case 'V':    lin+="8";    break;    case 'W':    case 'X':    case 'Y':    lin+="9";    break;    default:    break;    }        }       if (lin.length()==7) {    String newStr = lin.substring(0,3);        lin = newStr+"-"+lin.substring(3);}else {break;}        if (tm.containsKey(lin)) {               int count = tm.get(lin) + 1;                   tm.put(lin, count);               } else {                   tm.put(lin, 1);               }  }     Set se = tm.keySet();           Iterator it = se.iterator();           boolean flag = false;           while (it.hasNext()) {               String s = it.next().toString();               int count = tm.get(s);               if (count > 1) {                   flag = true;                   System.out.println(s + " " + count);               }           }         if (!flag) {               System.out.println("No duplicates. ");         }      }}


import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.*;class Main{    public static void main(String[] args) throws Exception{    //List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();     Map< String, Integer> tm = new TreeMap();    int cnt=0;    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    cnt = Integer.parseInt(in.readLine());    for(int i=0;i<cnt;i++){    String lin = "";    String str = in.readLine();    for(int j=0;j<str.length();j++){    if (Character.isDigit(str.charAt(j))) {                  lin+=str.charAt(j);                 continue;            }      char ch = str.charAt(j);    switch(ch){    case 'A':    case 'B':    case 'C':    lin+="2";    break;    case 'D':    case 'E':    case 'F':    lin+="3";    break;    case 'G':    case 'H':    case 'I':    lin+="4";    break;    case 'J':    case 'K':    case 'L':    lin+="5";    break;    case 'M':    case 'N':    case 'O':    lin+="6";    break;    case 'P':    case 'R':    case 'S':    lin+="7";    break;    case 'T':    case 'U':    case 'V':    lin+="8";    break;    case 'W':    case 'X':    case 'Y':    lin+="9";    break;    default:    break;    }        }       if (lin.length()==7) {    String newStr = lin.substring(0,3);        lin = newStr+"-"+lin.substring(3);}else {break;}        if (tm.containsKey(lin)) {               int count = tm.get(lin) + 1;                   tm.put(lin, count);               } else {                   tm.put(lin, 1);               }  }     Set se = tm.keySet();           Iterator it = se.iterator();           boolean flag = false;           while (it.hasNext()) {               String s = it.next().toString();               int count = tm.get(s);               if (count > 1) {                   flag = true;                   System.out.println(s + " " + count);               }           }         if (!flag) {               System.out.println("No duplicates.");           }      }}
好了,这个就是最终可以AC的代码了,希望对大家有所帮助,谢谢!

0 0
原创粉丝点击