487-3279 hoj 模擬AC

/*之前寫的在poj1002上AC了,但是在HOJ上過不了,現在又寫了一遍,在HOJ 上過了。
一個是字符串排序的寫法,可以當模板用。
其餘都是細節問題。*/
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100001;
int cmp(const void* _a,const void* _b)
{
    char* a=(char*)_a;
    char* b=(char*)_b;
    return strcmp(a,b);
}
char getnum(char c)
{
    if(c>='A'&&c<='C') return '2';
    else if(c<='F') return '3';
    else if(c<='I') return '4';
    else if(c<='L') return '5';
    else if(c<='O') return '6';
    else if(c<='S') return '7';
    else if(c<='V') return '8';
    else if(c<='Y') return '9';
}
char temp[maxn][50];
int main()
{
    int t;
    int ca=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,tt;
        if(ca>0) printf("\n");
        ca=1;
        memset(temp,0,sizeof(temp));
        scanf("%d",&n);
        getchar();
        char op[50];
        char t[50];
        memset(t,0,sizeof(t));
        for(int i=0; i<n; i++)
        {
            gets(op);
            tt=0;
            for(int j=0; j<strlen(op); j++)
            {
                if(op[j]=='-') continue;
                if(op[j]>='A'&&op[j]<='Z')
                    t[tt++]=getnum(op[j]);
                else t[tt++]=op[j];
            }
            t[tt]='\0';
            strcpy(temp[i],t);
        }
        qsort(temp,n,sizeof(temp[0]),cmp);
        int num=1;
        bool flag=false;
        for(int i=0; i<n; i++)
        {
            if(strcmp(temp[i],temp[i+1])==0)
            {
                num++;
                continue;
            }
            else
            {
                if(num>1)
                {
                    flag=true;
                    for(int j=0; j<3; j++)
                        printf("%c",temp[i][j]);
                    printf("-");
                    for(int j=3; j<strlen(temp[i]); j++)
                        printf("%c",temp[i][j]);
                    printf(" %d\n",num);
                }
                num=1;
            }
        }
        if(!flag) printf("No duplicates.\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章