zoukankan      html  css  js  c++  java
  • poj

    题意:问一些7位数(可能有前导0,可到达100000个)是否有重复,将重复的按字典序从小到大输出并输出重复次数,没有的话输出“No duplicates.”。

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

    ——>>本来题目很水的,在刷后缀数组,中间有个基数排序的东西,于是用基数排序来做这题。。。

    #include <cstdio>
    #include <cstring>
    #include <cctype>
    
    using namespace std;
    
    const int maxn = 100000 + 10;
    
    int n, p[maxn][8], figure[maxn];
    int c[15], sa[maxn], fa[maxn];       //sa[i]表示现在的第i位是原来的第sa[i]位
    int G[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
    
    void read() {
        for(int i = 0; i < n; i++) {
            int bit = 0;
            while(bit < 7) {
                char ch = getchar();
                if(ch == '
    ') break;
                if(ch != '-' && ch != 'Q' && ch != 'Z') {
                    if(isdigit(ch)) p[i][bit++] = ch - '0';
                    else p[i][bit++] = G[ch-'A'];
                }
            }
            if(bit == 7) while(getchar() != '
    ');
        }
    }
    
    void radixsort(){
        for(int i = 0; i < n; i++) fa[i] = i;       //初始化
        for(int bit = 6; bit >= 0; bit--) {       //第bit位
            memset(c, 0, sizeof(c));
            for(int i = 0; i < n; i++) c[p[i][bit]]++;      //数字出现的次数
            for(int i = 1; i <= 9; i++) c[i] += c[i-1];     //该数字最后一次出现应排在的位置
            for(int i = n-1; i >= 0; i--) sa[--c[p[fa[i]][bit]]] = fa[i];
            for(int i = 0; i < n; i++) fa[i] = sa[i];
        }
    }
    
    void solve() {
        radixsort();        //基数排序
        for(int i = 0; i < n; i++) {        //求十进制数
            figure[i] = 0;
            for(int j = 0; j < 7; j++) figure[i] = figure[i] * 10 + p[i][j];
        }
        bool ok = 0;        //是否有重复
        for(int i = 1; i < n; i++) if(figure[sa[i]] == figure[sa[i-1]]) {
            int cnt = 2;
            while(i+1 < n && figure[sa[i+1]] == figure[sa[i]]) {
                cnt++;
                i++;
            }
            for(int j = 0; j < 3; j++) printf("%d", p[sa[i]][j]);
            printf("-");
            for(int j = 3; j < 7; j++) printf("%d", p[sa[i]][j]);
            printf(" %d
    ", cnt);
            ok = 1;
        }
        if(!ok) puts("No duplicates.");
    }
    
    int main()
    {
        while(scanf("%d ", &n) == 1) {
            read();
            solve();
        }
        return 0;
    }
    
    


  • 相关阅读:
    jedis scan实现keys功能
    java简单实现一个阻塞式线程池
    Swift运算符
    数组的使用(1)
    Linux 常用命令
    Task02:基础查询与排序
    Task01:初识数据库
    摩尔投票法
    面向对象暑期课程总结
    xpath+requests+peewee——CSDN论坛全方位爬虫
  • 原文地址:https://www.cnblogs.com/pangblog/p/3359792.html
Copyright © 2011-2022 走看看