POJ1002-纯净水题,注意题目要求

本文介绍了一种使用哈希表处理字符串的方法,通过将输入的字符串转换为整数并记录重复出现的次数,来解决字符串匹配问题。代码示例中详细展示了如何定义哈希函数、处理特殊字符以及输出重复字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <cctype>
using namespace std;

int n;
char s[50];
int hash[10000000]; //一对一的hash表,10000000勉强能过,也可开个n大的表再排序;

int transform(char c)
{
    if (isdigit(c)) return c-'0';
    if (c<='C') return 2;
    if (c<='F') return 3;
    if (c<='I') return 4;
    if (c<='L') return 5;
    if (c<='O') return 6;
    if (c<='S') return 7;
    if (c<='V') return 8;
    if (c<='Y') return 9;
}

void chuli(char s[])
{
    int x=0,k=0;
    for (int i=0; s[i] && k<7; i++)
        if (isalpha(s[i]) || isdigit(s[i]) && s[i]!='Q' && s[i]!='Z')
        {
            k++;
            int y=transform(s[i]);
            x=x*10+y;
        }
    hash[x]++;
}

int main()
{
    scanf("%d",&n);
    getchar();
    for (int i=0; i<10000000; i++) hash[i]=0;
    for (int i=1; i<=n; i++)
    {
        gets(s);
        chuli(s);
    }
    bool mark=false;
    for (int i=0; i<10000000; i++)
    {
        if (hash[i]>1)
        {
            printf("%03d-%04d %d\n",i/10000,i%10000,hash[i]); //注意格式,如001-0000的情况
            mark=true;
        }
    }
    if (!mark) printf("No duplicates.\n"); //别忘了
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值