pku 2945 Find the Clones 字符串hash

题意:
给出n个字符串,统计出现1、2、..n次的字符串各有多少种
解法:
字符串hash,我用的强大无比的Java的HashMap,非常给力~
代码:
 1 import java.io.*;
 2 import java.util.*;
 3 public class Main {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) throws IOException{
 9         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
10         HashMap<String,Integer> refer=new HashMap<String,Integer>();
11         int cal[]=new int[20001];
12         while(true)
13         {
14             String tmp[]=in.readLine().split(" ");
15             if(Integer.parseInt(tmp[0])==0&&Integer.parseInt(tmp[1])==0break;
16             int n=Integer.parseInt(tmp[0]);
17             refer.clear();
18             for(int i=0;i<n;i++)
19             {
20                 String str=in.readLine();
21                 if(refer.containsKey(str)) refer.put(str,refer.get(str)+1);
22                 else refer.put(str, 1);
23             }
24             Arrays.fill(cal,0);
25             for(int i:refer.values())
26               cal[i]++;
27             for(int i=1;i<=n;i++)
28                 System.out.println(cal[i]);
29             
30         }
31 
32     }
33 
34 }

posted on 2010-12-10 16:55 yzhw 阅读(167) 评论(0)  编辑 收藏 引用 所属分类: simple problem~string algorithm


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜