ACM 外星人 关于STL map的困惑

来源:互联网 发布:php编译安装pdo mysql 编辑:程序博客网 时间:2024/04/26 19:51


寻找副本

TimeLimit: 2 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 401   Accepted: 86  

Description

得克萨斯州的一个小镇Doubleville,被外星人攻击。他们绑架了当地人并把他们带到飞船里。经过一番(相当不愉快的)人体实验,外星人克隆了一些受害者,并释放了其中的多个副本回Doubleville。所以,现在有可能发生有6个人:原来的人和5个复制品都叫做Hugh F. Bumblebee。现在FBUC美国联邦调查局命令你负责确定每个人被复制了多少份,为了帮助您完成任务,FBUC收集每个人的DNA样本。同副本和原来的人具有相同的DNA序列,不同的人有不同的序列(我们知道,城里没有同卵双胞胎,这不是问题)

Input

输入中含有多组数据,每一组以一行n m开始,表示共有n个人1 ≤ n ≤ 20000,其中DNA序列长度为m, 1 ≤ m ≤ 20. 接下来的n行为DNA序列:每行包含m个字符,字符为'A','C','G'或'T'。 输入以n=m=0 结尾。

Output

每一组数据应输出n行,每行一个整数。第一行表示有几个人没有被复制,第二行表示有几个人只被复制一次(也就是说有两个相同的人),第三行表示有几个是被复制两次,依次类推,第i行表示其中有i个相同的人的共有几组。举例来说,如果有11个样本,其中之一是John Smith,和所有其余的都是从Joe Foobar复制来的副本,那么你必须打印第一行和第10行输出1,其余行输出0。 

Sample Input

9 6
AAAAAA
ACACAC
GTTTTG
ACACAC
GTTTTG
ACACAC
ACACAC
TCCCCC
TCCCCC
0 0

Sample Output

1
2
0
1
0
0
0
0
0

 

          一看到题,马上想到map可以很好的解决。灰常的简单,果断的WA。

用map写的wa代码:

复制代码
 1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include <iterator> 5 #include <cstdio> 6 using namespace std; 7  8 map<string,int> m; 9 int cnt[21000];10 11 int main()12 {13       string str;14       int i,num,len;15       16       while(scanf("%d%d",&num,&len) &&( num + len ))        17       {18          for( i = 0 ; i < num ; ++i)19          {20                cin>>str;                              21                m[str]++;22                cnt[i+1] = 0;23          }              24          25          map<string,int>::iterator it; 26          27          for( it = m.begin();it != m.end(); ++it)28          cnt[it->second]++;29          30          for(i = 1 ; i <= num ; ++i)31          printf("%d\n",cnt[i]);32       }33       34       return 0;35 }
复制代码

  

   为什么会是wa啊???样例测试数据当然是过了。思路是不可能错的啊???map会有什么特别需要注意的地方吗???

   郁闷之极,就用C语言写了下(算法没变)。瞬间AC。可见我的思路没有问题。那么哪儿错了呢?

 

AC代码(纯C):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct dna
{
  char ch[21];      
} DNA[21000];
 
int cnt[21000];
 
int cmp(const void *a,const void *b)
{
  struct dna *x = (struct dna *)a;
  struct dna *y = (struct dna *)b;
  return strcmp(x->ch,y->ch);   
}
 
int main()
{
  int num,len,i,t;
     
    while(scanf("%d%d",&num,&len) && (num + len))
    {
        for(i = 0 ; i < num ; ++i)
        {
              scanf("%s",DNA[i].ch);                             
              cnt[i+1] = 0;
        
         
        qsort(DNA,num,sizeof(DNA[0]),cmp);
         
        t = 1;
        for(i = 1 ; i < num ; ++i)
        {
            if( !strcmp(DNA[i].ch,DNA[i-1].ch))     
            ++t;
            else
            {
               cnt[t]++;
               t = 1;    
            
        }        
        cnt[t]++;
         
        for(i = 1 ; i <= num ; ++i)
        printf("%d\n",cnt[i]);
    }
     
    return 0;
}

 

 

 

        map为什么会WA 呢???

        路过的大牛指点下啊!!!

 

        某一天再来想想!!!

 

 

4月19日

原来map没问题,是我忘记清空了,郁闷!!!

以后只要用STL,就必定看看是否要清空!!!

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
 #include <map>
 #include <string>
 #include <iterator>
 #include <cstdio>
 using namespace std;
  
 map<string,int> m;
 int cnt[21000];
  
 int main()
 {
       string str;
       int i,num,len;
        
       while(scanf("%d%d",&num,&len) &&( num + len ))       
       {
          for( i = 0 ; i < num ; ++i)
          {
                cin>>str;                             
                m[str]++;
                cnt[i+1] = 0;
                str.clear();
          }             
           
          map<string,int>::iterator it;
           
          for( it = m.begin();it != m.end(); ++it)
           cnt[it->second]++;
          
          for(i = 1 ; i <= num ; ++i)
          printf("%d\n",cnt[i]);
          m.clear();
       }
        
        
       return 0;
 }

 

 


 http://www.cnblogs.com/HpuAcmer/archive/2012/03/31/2427704.html

记录下在IT路上的成长足迹!!!
原创粉丝点击