POJ1002(字符串+map大法+模拟)

题目链接:http://poj.org/problem?id=1002


解题思路:

此题有坑,刚开始输入字符串是一定要用char *,用string的话会超时。

思路很简单,就是边输入边处理,将字符串处理成要求格式。这里用到map大法,各种hash映射。最后记得要排下序(升序),如果没有满足条件的话,要输出No duplicates.


完整代码:

//#include <bits/stdc++.h>
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;


/*
__gcd(a, b)  // My gcd
__builtin_ffs(x) //My lowbit
__builtin_popcount(x) //The number of 1-bits
*/

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int maxn = 100001;
char a[maxn];
map<char , char> m;
map<int , string> s;
map<string , int>vis;
string result[maxn];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    m['A'] = m['B'] = m['C'] = '2';
    m['D'] = m['E'] = m['F'] = '3';
    m['G'] = m['H'] = m['I'] = '4';
    m['J'] = m['K'] = m['L'] = '5';
    m['M'] = m['N'] = m['O'] = '6';
    m['P'] = m['R'] = m['S'] = '7';
    m['T'] = m['U'] = m['V'] = '8';
    m['W'] = m['X'] = m['Y'] = '9';
    int n;
    while(cin >> n)
    {
        s.clear();
        vis.clear();
        int cnt = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            cin >> a;
            //cin >> a[i];
            string t = "";
            for(int j = 0 ; a[j] ; j ++)
            {
                if(a[j] == '-')
                    continue;
                else if(a[j] >= '0' && a[j] <= '9')
                    t += a[j];
                else
                    t += m[a[j]];
            }
            if(vis[t] == 0)
            {
                s[cnt ++] = t;
                vis[t] ++;
            }
            else
                vis[t] ++;
        }
        int tt = 0;
        for(int i = 0 ; i < cnt ; i ++)
        {
            if(vis[s[i]] > 1)
            {
                result[tt ++] = s[i];
            }
        }
        if(tt == 0)
        {
            printf("No duplicates.\n");
            continue;
        }
        sort(result , result + tt);
        for(int i = 0 ; i < tt ; i ++)
        {
            for(int j = 0 ; j <=2 ; j ++)
                cout << result[i][j];
            cout << "-";
            for(int j = 3 ; j < 7 ; j ++)
                cout << result[i][j];
            cout << " " << vis[result[i]] << endl;
        }
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值