MDeath-Kid

- M I T & Y
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

POJ 1002

Posted on 2011-05-29 09:32  MDeath-Kid  阅读(538)  评论(2编辑  收藏  举报

标准一字符串处理的题,看到题马上想到的是map<string,int>,然后果断超时,而后改成的map<int,int>,WA了,查查结题报告,发现输出要控制宽度,不懂!望大家给解释,时间是960MS

/*
8701557MDK1002Accepted3992K954MSG++1605B2011-05-29 08:46:42
*/
#include<stdio.h>
#include<iostream>
#include<limits.h>

#include<string.h>
#include<math.h>
#include<time.h>
#include<algorithm>
#include<map>


#define N 1005
using namespace std;
char hash(char a)
{
    if(a>='A'&&a<='C')
        return '2';
    else if(a>='D'&&a<='F')
        return '3';
    else if(a>='G'&&a<='I')
        return '4';
    else if(a>='J'&&a<='L')
        return '5';
    else if(a>='M'&&a<='O')
        return '6';
    else if(a>='P'&&a<='S')
        return '7';
    else if(a>='T'&&a<='V')
        return '8';
    else if(a>='W'&&a<='Y')
        return '9';
}
int main()
{
    int n;
    map<int,int> mpt;
    map<int,int>::iterator p;
    scanf("%d",&n);
    getchar();
    char sc[8];
    char sss[100];
    int k=0;
    for(int j=0;j<n;j++)
    {
        k=0;
        scanf("%s",sss);
        for(int i=0;i<strlen(sss);i++)
        {
            if(sss[i]>='A'&&sss[i]<='Z')
            {
                sc[k++]=hash(sss[i]);
            }
            else if(sss[i]=='-')
            {
            }
            else
            {
                sc[k++]=sss[i];
            }
        }
        //cout<<stmp<<endl;
        //fflush(stdin);
        getchar();
        mpt[atoi(sc)]++;
    }
    //cout<<"----"<<endl;
    bool flag=1;
    for(p=mpt.begin();p!=mpt.end();p++)
    {
        if(p->second!=1)
        {
            flag=0;
            printf("%03d-%04d %d\n",p->first/10000,p->first%10000,p->second);
        }//03和04没加上就是WA的地方
    }
    if(flag)
        printf("No duplicates.\n");
    mpt.clear();
}
/*
网上一代码
Accepted1100K516MSG++1482B
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
inline void InitMap(char *map)
{    
     for (int i = 0; i <= 9; i++)
       map[i + '0'] = i;
     map['A'] = map['B'] = map['C'] = 2;
     map['D'] = map['E'] = map['F'] = 3;
     map['G'] = map['H'] = map['I'] = 4;
     map['J'] = map['K'] = map['L'] = 5;
     map['M'] = map['N'] = map['O'] = 6;
     map['P'] = map['R'] = map['S'] = 7;
     map['T'] = map['U'] = map['V'] = 8;
     map['W'] = map['X'] = map['Y'] = 9;
 }
 int cmp(const void *a, const void *b)
 {
     return *(int *)a - *(int *)b;
 }
 int main()
 {
     char map[256];
     char str[256];
     int n, i, j, t;
     int *num;
     bool flag = true;
     InitMap(map);
     cin >> n;
     num = new int[n];
    for (i = 0; i < n; i++)
     {
         scanf("%s", str);
         t = 0;
         for (j = 0; str[j] != '\0'; j++)
         {
             if (str[j] == '-') continue;
             t = t * 10 + map[str[j]];
         }
         num[i] = t;
     }
     qsort(num, n, sizeof(int), cmp);
     i = 0;
     do
    {
         j = i;
         while (i < n && num[++i] == num[j]);
         if (i - j  > 1)
         {
             printf("%03d-%04d %d\n", num[j] / 10000, num[j] % 10000, i - j);
             flag = false;
         }
     } while (i < n);
     delete [] num;
     if (flag)
         printf("No duplicates.\n");
     return 0;
 }