Description
Input
Output
Sample Input
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
Sample Output
310-1010 2 487-3279 4 888-4567 3
#include <stdio.h> #include <stdlib.h> #include <string.h> int arr[1000000]; int cmp(const void * a,const void * b){ return *(int *)a - *(int *)b; } int div2[8] = {1000000,100000,10000,1000,100,10,1}; int stoi(char * p){ int j; int len = strlen(p); int sum = 0; for(j=0; j<len; j++){ if( p[j] != '-' ){ sum *= 10; if(p[j] >= 'A' && p[j] <= 'Y') sum += (p[j] - 'A' - (p[j] > 'Q')) / 3 + 2; else if(p[j] >= '0' && p[j] <= '9') sum += p[j] - '0'; } } return sum; } int main(void) { //freopen("in.txt","r",stdin); int n,i; while(scanf("%d", &n) != EOF){ char temp[100]; for( i=0; i<n; i++){ scanf("%s",temp); arr[i] = stoi(temp); } int flag = 0; qsort(arr, n, sizeof(int), cmp); int cnt = 0; for( i=1; i<=n; i++){ if(arr[i] == arr[i-1]){ cnt ++; flag=1; } else{ if(cnt){ int j; int tmp = arr[i-1]; for( j=0; j<7; j++){ printf("%d",tmp / div2[j]); tmp %= div2[j]; if(j == 2) printf("-"); } printf(" %d\n",cnt+1); } cnt = 0; } } if( !flag ) printf("No duplicates. \n"); } return 0; }
下面使用字典树,解决,较为麻烦。
经常测试,没有太大的改进。运行时间很接近。
因为这里并没有用到字典树的优势,就是统计所有前缀。
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; typedef struct trietree * Ptree; struct trietree { bool arrive; int treenum; Ptree next[10]; } node[1000000]; int size; bool findsolve; int dispose(char * p); void addnum(int num); //增加数字串 void newtree(int no); void dfs(char phone[9], int m, Ptree p); int arr[100000]; int main() { //freopen("in.txt","r",stdin); int n; //串的个数 int i, j; int number; char phone[9]; char ch[80]; while (scanf("%d", &n) != EOF) { //scanf("%d", &n); findsolve = false; //是否有重复的串 size = 1; newtree(1); for (i = 0; i < n; i++) { scanf("%s", ch); number = dispose(ch); arr[i] = number; //cout << number << endl; addnum(number); } dfs(phone, 0, &node[1]); //遍历 if (!findsolve) printf("No duplicates. \n"); } return 0; } void newtree(int no) { int i; node[no].arrive = false; node[no].treenum = 0; for (i = 0; i <= 9; i++) node[no].next[i] = NULL; } void addnum(int num) { Ptree p = &node[1]; int i, k; for (i = 0; i <= 6; i++) { k = num % 10; num /= 10; if (!p->next[k]) { newtree(++size); p->next[k] = &node[size]; } p = p->next[k]; } p->arrive = true; //只有叶子节点的 arrive是true p->treenum++; } void dfs(char phone[9], int m, Ptree p) { if (true == p->arrive) { //如果该节点存在数 if (p->treenum > 1) { for (int i = 1; i <= 7; i++) { if (i == 4) printf("-"); printf("%c", phone[i]); } printf(" %d\n", p->treenum); findsolve = true; } return; } for (int i = 0; i <= 9; i++) { if (p->next[i]) { phone[m + 1] = i + '0'; //cout << phone[m+1] << endl; dfs(phone, m + 1, p->next[i]); } } } int dispose(char * p) { int num = 0; char * q = p; while (*++p != '\0') ; p--; while (p >= q) { if (*p == '-') { p--; continue; } num *= 10; if (*p >= 'A' && *p <= 'Y') { num += (*p - 'A' - (*p > 'Q')) / 3 + 2; } else if (*p >= '0' && *p <= '9') num += *p - '0'; p--; } return num; }