泪眼成诗

导航

POJ1002

此题先前搞了好几天,未果,留着,今天终于发现问题所在,原来是题意理解有问题。在所有号码都没有重复的情况下输出No duplicate。 我理解为只要这个号码没有重复就输出No duplicate。悲剧啊 。

代码1:耗时》1500MS 用的是快速排序qsort,在这里对qsort有了进一步的了解。如果将字符串先转换为数字,在快速排序时间可以可以达到500MS左右。代码三为此方法.

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

1.这里的void* 是可以指向任何指针的,之前一直认为只能指向一维的。惭愧啊!

2. int ( * comparator ) ( const void *, const void * ) 这个函数指针的变量const void*是与void *base同样的指针。qsort是用指针来跟踪要排序的数据。所以在compare函数里面要进行强制转换。此处链接为各种强制转换的实例http://blog.csdn.net/l04205613/archive/2010/06/15/5671843.aspx

代码如下:

代码
1 #define _CRT_SECURE_NO_DEPRECATE
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6  #define MAX 50
7
8  void Show(const char **pstr,long int len);
9
10
11  int Cmp(const void *a, const void *b)
12 {
13 const char **a1 = (const char **)a;
14 const char **b1 = (const char **)b;// 注意此处的强制转换
15
16 return strcmp(*a1,*b1);
17 }
18
19
20 int main(void)
21 {
22 long num;
23 long i;
24 int k,j;
25 int len;
26 char (*str)[MAX];
27 char temp[MAX];
28 char **pstr;
29
30 scanf("%ld",&num);
31
32 str = (char(*)[MAX])malloc(sizeof(char[MAX])*num);
33 pstr = (char **)malloc(sizeof(char*)*num);
34
35 for(i = 0; i<num; i++)
36 {
37 scanf("%s",temp);
38
39 for(j = 0,k=0;j<strlen(temp);j++)
40 {
41 if(temp[j] == '-')
42 continue;
43 str[i][k++] = temp[j];
44 }
45 str[i][k] = '\0';
46 pstr[i] = str[i];
47 }
48
49
50 for(i=0; i<num;i++)
51 {
52 len = strlen(str[i]);
53
54 for(j=0; j<len; j++)
55 {
56 switch(str[i][j])
57 {
58 case 'A':
59 case 'B':
60 case 'C':str[i][j] = '2';break;
61 case 'D':
62 case 'E':
63 case 'F':str[i][j] = '3';break;
64 case 'G':
65 case 'H':
66 case 'I':str[i][j] = '4';break;
67 case 'J':
68 case 'K':
69 case 'L':str[i][j] = '5';break;
70 case 'M':
71 case 'N':
72 case 'O':str[i][j] = '6';break;
73 case 'P':
74 case 'R':
75 case 'S':str[i][j] = '7';break;
76 case 'T':
77 case 'U':
78 case 'V':str[i][j] = '8';break;
79 case 'W':
80 case 'X':
81 case 'Y':str[i][j] = '9';break;
82 default:break;
83
84 }
85 }
86 }
87
88 qsort((void*)pstr,num,sizeof(char*),Cmp);
89
90
91 Show(pstr,num);
92
93 free(str);
94 free(pstr);
95
96 return 0;
97 }
98
99
100 void Show(const char **pstr,long int len)
101 {
102 long int i;
103 int j;
104 int count = 1;
105 int flag = 1;
106
107
108 for(i = 0; i<len; i++)
109 {
110
111 while(i<len-1 && strcmp(pstr[i],pstr[i+1]) == 0)
112 {
113 count++;
114 i++;
115 }
116
117 if(count > 1)
118 {
119 for(j = 0;j<7;j++)
120 {
121 if(j == 3)
122 putchar('-');
123 printf("%c",pstr[i][j]);
124 }
125 printf(" %d\n",count);
126 count = 1;
127 flag = 0;
128 }
129 }
130 if(flag)
131 printf("No duplicates.\n");
132
133 }
134

第二种方法用的是哈希法 不过不成熟 耗时》700MS

 

代码
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #define MAX 256
6
7 int hash[10000000];
8
9 int main(void)
10 {
11
12 char map[26]={'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'};
13 char str[MAX];
14 char temp[MAX];
15 long num;
16 long i,j;
17 //int len;
18 int k;
19 int data;
20 long min = 10000000;
21 long max = 0;
22 int head = 0;
23 int rear = 0;
24 int flag = 1;
25
26
27
28 scanf("%d",&num);
29
30 for(i = 0;i<num;i++)
31 {
32 scanf("%s",temp);
33
34 for(j=0,k=0;j<strlen(temp);j++)
35 {
36 if(temp[j] >= 'A'&& temp[j] <= 'Z')
37 {
38 str[k++] = map[temp[j]-'A'];
39 }
40 else if(temp[j]>='0'&& temp[j]<='9')
41 str[k++] = temp[j];
42 else
43 continue;
44
45 }
46
47 str[k] = '\0';
48
49 data = atoi(str);
50
51 hash[data]++;
52
53 if(max < data)
54 max = data;
55 if(min > data)
56 min = data;
57 }
58
59 for(i = min;i<=max;i++)
60 {
61 if(hash[i] > 1)
62 {
63 head = i/10000;
64 rear = i%10000;
65
66 printf("%03d-%04d %d\n",head,rear,hash[i]);
67 flag = 0;
68 }
69
70 }
71 if(flag)
72 printf("No duplicates.\n");
73
74
75 return 0;
76 }

 此法为把字符转换为数字在排序,效率提高很多,此代码来自刘大师:

代码三:

代码
1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 using namespace std;
7 inline void InitMap(char *map)
8 {
9 for (int i = 0; i <= 9; i++)
10 map[i + '0'] = i;
11 map['A'] = map['B'] = map['C'] = 2;
12 map['D'] = map['E'] = map['F'] = 3;
13 map['G'] = map['H'] = map['I'] = 4;
14 map['J'] = map['K'] = map['L'] = 5;
15 map['M'] = map['N'] = map['O'] = 6;
16 map['P'] = map['R'] = map['S'] = 7;
17 map['T'] = map['U'] = map['V'] = 8;
18 map['W'] = map['X'] = map['Y'] = 9;
19 }
20 int cmp(const void *a, const void *b)
21 {
22 return *(int *)a - *(int *)b;
23 }
24 int main()
25 {
26 char map[256];
27 char str[256];
28 int n, i, j, t;
29 int *num;
30 bool flag = true;
31 InitMap(map);
32 cin >> n;
33 num = new int[n];
34 for (i = 0; i < n; i++)
35 {
36 scanf("%s", str);
37 t = 0;
38 for (j = 0; str[j] != '\0'; j++)
39 {
40 if (str[j] == '-') continue;
41 t = t * 10 + map[str[j]];
42 }
43 num[i] = t;
44 }
45 qsort(num, n, sizeof(int), cmp);
46 i = 0;
47 do
48 {
49 j = i;
50 while (i < n && num[++i] == num[j]);
51 if (i - j > 1)
52 {
53 printf("%03d-%04d %d\n", num[j] / 10000, num[j] % 10000, i - j);
54 flag = false;
55 }
56 } while (i < n);
57 delete [] num;
58 if (flag)
59 printf("No duplicates.\n");
60 return 0;
61 }
62

 

posted on 2010-08-28 15:58  泪眼成诗  阅读(2324)  评论(0编辑  收藏  举报