poj - 1002 - 487-3279(基数排序)

来源:互联网 发布:招商银行待遇知乎 编辑:程序博客网 时间:2024/06/06 16:45

题意:问一些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 == '\n') 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() != '\n');    }}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\n", cnt);        ok = 1;    }    if(!ok) puts("No duplicates.");}int main(){    while(scanf("%d ", &n) == 1) {        read();        solve();    }    return 0;}