Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

STL的hash_map就是爽撒。。贴AC代码

Posted by yzhw at 2009-10-24 09:36:04 on Problem 1119
# include <iostream>
#include <ext/hash_map>
# include <string>
# include <cstring>
# include <cstdio>
# include <cmath>
using namespace std;
using namespace __gnu_cxx;
struct str_hash
{
    size_t operator()(const string& str) const
    {
        return __stl_hash_string(str.c_str());
    }
};
char *deal(char *pos)
{
    for(int i=0;i<strlen(pos);i++)
    {
       if(pos[i]>='A'&&pos[i]<='Z')
         pos[i]+=32;
       else if(pos[i]>='a'&&pos[i]<='z');
       else if(pos[i]>='0'&&pos[i]<='9');
       else
       {
           for(int j=i+1;j<=strlen(pos);j++)
              pos[j-1]=pos[j];
           i--;
       }
    }
    return pos;
}
using namespace __gnu_cxx;
int main()
{
    char line[300];
    bool one=1;
    hash_map<string,int,str_hash> l1,l2;
    l1.clear();
    while(1)
    {
         double total=0;
         l2.clear();
         gets(line);
         if(!strcmp(line,"----------")) break;
         while(strcmp(line,"----------")!=0)
         {
            char *p=strtok(line," ");
            string temp;
            if(p)
            {
            temp=string(deal(p));
            if(temp.length())
            {
               if(one)
               {
                  if(l1.find(temp)!=l1.end())
                     l1[temp]++;
                  else
                     l1[temp]=1;
               }
               else
               {
                  if(l2.find(temp)!=l2.end())
                     l2[temp]++;
                  else
                     l2[temp]=1;
               }
            }
            }
            while((p=strtok(NULL," ")))
            {
               temp=string(deal(p));
               if(temp.length())
                {
                   if(one)
                   {
                      if(l1.find(temp)!=l1.end())
                         l1[temp]++;
                      else
                         l1[temp]=1;
                   }
                   else
                   {
                      if(l2.find(temp)!=l2.end())
                         l2[temp]++;
                      else
                         l2[temp]=1;
                   }
                }
            }
            gets(line);
         }
         if(one)
           one=0;
         else
         {
           for(hash_map<string,int,str_hash>::iterator it=l2.begin();it!=l2.end();it++)
           {
              hash_map<string,int,str_hash>::iterator t=l1.find(it->first);
              if(t!=l1.end())
              {
                 total+=sqrt((double)(it->second)*(t->second));
              }
           }
         printf("%.2f\n",total);
         }
    }
  // system("pause");
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator