POJ2002 第一次与map亲密接触

来源:互联网 发布:淘宝旺旺红包怎么收 编辑:程序博客网 时间:2024/05/22 10:45

题目大意:呜呜~~我承认~~字符串的题目陷阱很多~~~Exactly seven of the characters in the string will be digits or letters~~说明给的每个串的里面有意义的字符是确切的7个,但是不排除有其余的字符。另外,scanf是无法输入string的 ,因为c++不能兼容C~~另外按个来的话,string是可以赋值给char的,但是整串是不允许的,但是char是可以整串赋值给string的。

思路:用map,很慢~~在G++竟然TIE,在C++更无语了,我竟然没加string竟会编译错误~~囧。。各种水啊 ~~。

话说map,第一次啊~~呵呵~~map自动按照关键值排序立了,关键值是first

AC program:#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<math.h>#include<iomanip>#include<queue>#include<map>using namespace std;char cc[1000],tmp[30];int kg; map<string ,int> mm;string str; int fn(char p){if(p=='A'|| p=='B'|| p=='C' )return  2 ;if(p=='D'|| p=='E'|| p=='F' )return  3 ;if(p=='G'|| p=='H'|| p=='I' )return  4 ;if(p=='J'|| p=='K'|| p=='L' )return  5 ;if(p=='M'|| p=='N'|| p=='O' )return  6 ;if(p=='P'|| p=='R'|| p=='S' )return  7 ;if(p=='T'|| p=='U'|| p=='V' )return  8 ;if(p=='W'|| p=='X'|| p=='Y' )return  9 ;   }int main(){int n,len;scanf("%d",&n); for(int i=0;i<n;i++){   scanf("%s",cc);    len=strlen(cc);   kg=0;   //memset(tmp,0,sizeof(tmp));    for(int g=0;g<len;g++)   {      if(cc[g]=='-')continue;      if(isupper(cc[g])){ //cc[g]>='A'&&cc[g]<='Z'                          tmp[kg]='\0';                          tmp[kg++]=fn(cc[g])+'0';                         }       else  tmp[kg++]=cc[g];        }   tmp[kg]='\0';// 其实包括在上面的这个操作,原来 我是用memset的    //但是想试一下水就这样了-_-    str=tmp;   mm[str]++;        }map<string ,int >::iterator p;int flag=0; for(p=mm.begin();p!=mm.end();p++){  if(p->second>1)  {    flag=1;    string strrr=p->first;    int i=0;     for(;i<3;i++)    {                 printf("%c",strrr[i]);     }        printf("-");     for(;i<7;i++)//strrr.length()     {                                printf("%c",strrr[i]);     }                  printf(" %d\n",p->second);   }                                 }if(!flag)   printf("No duplicates.\n"); system("pause"); return 0;}