您的位置:首页 > 编程语言 > Java开发

poj 1002 Java AC啦

2017-11-12 12:35 281 查看
poj1002题目:http://poj.org/problem?id=1002

偷偷告诉自己:想用Java来写算法题目,以后有实力就参加比赛。。。。。。。

本题还是比较简单的,但是简单的题,用Java来解决还是用了不少东西。

本题涉及的内容:

使用String的substring()和replaceAll()

使用TreeMap使得key有序,按照字符串排序的规则来排序电话号码,刚好满足要求

使用iterator()来遍历key,通过key得到value,判断value是否大于1来决定输出

已经ac的源码(纯自己写的,没有借鉴任何人,但是后来发现和网上其他人差不多,毕竟简单题嘛):

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
TreeMap<String,Integer> list=new TreeMap<String,Integer>();
Set<String> set;
Iterator<String> iter;
int n,i;
boolean flag;
String phone,temp;
char[] ch;
n=sc.nextInt();
while(n>0) {
phone=sc.next();
phone=phone.replaceAll("-", "");
ch=phone.toCharArray();
for(i=0;i<ch.length;i++) {
if(ch[i]>='A'&&ch[i]<='O') {
ch[i]=(char) ((ch[i]-'A')/3+'0'+2);
}else if(ch[i]>='P'&&ch[i]<='S') {
ch[i]='7';
}else if(ch[i]>='T'&&ch[i]<='V') {
ch[i]='8';
}else if(ch[i]>='W'&&ch[i]<='Y') {
ch[i]='9';
}
}
phone=new String(ch);
if(list.containsKey(phone)) {
list.put(phone, list.get(phone)+1);
}else {
list.put(phone,1);
}
n--;
}
set=list.keySet();
iter=set.iterator();
flag=false;
while(iter.hasNext()) {
temp=iter.next();
if(list.get(temp)>1) {
flag=true;
System.out.println(temp.substring(0, 3)+"-"+temp.substring(3)+" "+list.get(temp));
}
}
if(!flag) {
System.out.println("No duplicates.");
}
sc.close();
}
}


刚开始,在System.out.println(temp.substring(0, 3)+"-"+temp.substring(3)+" "+list.get(temp))这里我用的是for:

for(i=0;i<3;i++){
System.out.print(temp.charAt(i));
}
System.out.print("-");
for(i=3;i<temp.length();i++){
System.out.print(temp.charAt(i));
}

虽然看起来是有点傻,但是开始就是没想到substring嘛,怪我喽,再说我敲代码从来都是神速敲完。

后来知道了substring,但是我感觉substring的jdk源码应该也是循环实现,毕竟这个实现也简单。于是又找其他原因,但是看看代码,实在是感觉没什么可以修改和简化的了,于是做了一个里程碑式的决定,改用substring。抱着试试的心态,提交了,竟然,竟然神奇般的AC了,难道真的要用substring?难道substring内部不是循环吗。

我不服啊,我要看看,所以在eclipse里面一直Ctrl+鼠标点击,找源码。

案发情况如下:

第一次点击substring,到这里了:

public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}


一看就是几个if,不会对算法时间有什么影响的,肯定是接着找new String(value, beginIndex, subLen),到这里:

public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}


还是几个if,无伤大雅,接着找Arrays.copyOfRange(value, offset, offset+count),到这里:

public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}


还没到底,接着找System.arraycopy,到这里:

public static native void arraycopy(Object src,  int  srcPos,
Object dest, int destPos,
int length);


终于到底了,这是什么?native?不是循环吗,本地方法很快吗?
本地又是怎么实现的?能快多少呢?

原谅我是菜鸟,这些都不知道,谁知道告诉我一声,谢谢大兄弟。

但是,总之,就用了substring,就AC了,可能真的会稍微快点吧。不过我记住了,以后肯定用substring。

继续加油喽!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 1002 Java