您的位置:首页 > 其它

POJ1002_487-3279(快速排序)

2014-01-06 15:48 288 查看
487-3279

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 227719 Accepted: 39710
Description

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of
the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza
Hut by calling their ``three tens'' number 3-10-10-10. 

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: 

A, B, and C map to 2 

D, E, and F map to 3 

G, H, and I map to 4 

J, K, and L map to 5 

M, N, and O map to 6 

P, R, and S map to 7 

T, U, and V map to 8 

W, X, and Y map to 9 

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. 

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) 

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. 

Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number
alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. 

Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange
the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: 

No duplicates. 

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

Source

East Central North America 1999

解题报告

表示一直RE。。。未知错误。。。

先说说RE之后的TLE,可能排序太慢了。。。之前一直不会用快排函数,升降序的函数也不清楚。。。

查了好多资料才明白。。。

void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));


int cmp(const void *a,const void *b)//对于int类型的排序
{
//降序排序
//return(*(int *)a-*(int *)b);
//升序排序
return(*(int *)b-*(int *)a);
}
int cmp(const void *p1,const void *p2)//对于char类型的排序
{
//降序排序
//return strcmp((char *)p2,(char *)p1);
//升序排序
return strcmp((char *)p1,(char *)p2);
}
TLE过了就返回RE了,何解。。。

全局开了,数组也清零了。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
using namespace std;
int cmp(const void *p1,const void *p2)//对于字符串的排序
{
//降序排序
//return strcmp((char *)p2,(char *)p1);
//升序排序
return strcmp((char *)p1,(char *)p2);
}
char c[100],str1[100],str2[100000][100];
int main()
{
int i,j,k,f;
int n,x[100];
memset(str2,0,sizeof(str2));
memset(x,0,sizeof(x));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",str1);
for(j=0,k=0;str1[j]!='\0';j++)
{
if(str1[j]!='-')
{
if(str1[j]>='A'&&str1[j]<'Q')
str2[i][k]=(str1[j]-'A')/3+2+'0';
else if(str1[j]>'Q'&&str1[j]<='Y')
str2[i][k]=(str1[j]-'A'-1)/3+2+'0';
else if(str1[j]>='0'&&str1[j]<='9')
str2[i][k]=str1[j];
k++;
}
}
}
qsort(str2,n+1,sizeof(str2[1]),cmp);
/*for(i=1;i<=n;i++)
puts(str2[i]);*/
f=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str2[i],str2[j])==0)
x[i]++;
}
x[i]+=1;
if(x[i]>=2&&strcmp(str2[i],str2[i-1])!=0)
{
printf("%c%c%c-%c%c%c%c %d\n",str2[i][0],str2[i][1],str2[i][2],str2[i][3],str2[i][4],str2[i][5],str2[i][6],x[i]);
f=1;
}
}
if(f==0)printf("No duplicates.\n");
return 0;
}


我快奔溃了,修改了下数据大小,把另外的一个x数组开成全局的,结果是TLE。。。
期间还出现过Memory Limit Exceede 还能不能好好的写代码呢。。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *p1,const void *p2)
{
return strcmp((char *)p1,(char *)p2);
}
char c[100],str1[100],str2[100000][100];
int x[100000];
int main()
{
int i,j,k,f;
int n;
memset(str2,0,sizeof(str2));
memset(x,0,sizeof(x));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",str1);
for(j=0,k=0;str1[j]!='\0';j++)
{
if(str1[j]!='-')
{
if(str1[j]>='A'&&str1[j]<'Q')
str2[i][k]=(str1[j]-'A')/3+2+'0';
else if(str1[j]>'Q'&&str1[j]<='Y')
str2[i][k]=(str1[j]-'A'-1)/3+2+'0';
else if(str1[j]>='0'&&str1[j]<='9')
str2[i][k]=str1[j];
k++;
}
}
}
qsort(str2,n+1,sizeof(str2[1]),cmp);
/*for(i=1;i<=n;i++)
puts(str2[i]);*/
f=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str2[i],str2[j])==0)
x[i]++;
}
x[i]+=1;
if(x[i]>=2&&strcmp(str2[i],str2[i-1])!=0)
{
printf("%c%c%c-%c%c%c%c %d\n",str2[i][0],str2[i][1],str2[i][2],str2[i][3],str2[i][4],str2[i][5],str2[i][6],x[i]);
f=1;
}
}
if(f==0)printf("No duplicates.\n");
return 0;
}
小激动,看了大神用的都是什么树呀,什么STL的,什么查找数,红黑树,表示都不会,我还是乖乖排序的。。。
表示快排应该不会出错,可能是快排后面有个嵌套循环很花时间。。。
网上看看大神写的,终于找到用排序写的。。。
把嵌套循环换一个方式写,终于过了。。。用时750ms。。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *p1,const void *p2)
{
return strcmp((char *)p1,(char *)p2);
}
char c[100],str1[100],str2[100000][100];
//int x[100000];
int main()
{
int i,j,k,f,num;
int n;
memset(str2,0,sizeof(str2));
//memset(x,0,sizeof(x));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",str1);
for(j=0,k=0;str1[j]!='\0';j++)
{
if(str1[j]!='-')
{
if(str1[j]>='A'&&str1[j]<='Y')
str2[i][k]=(str1[j]-'A'-(str1[j]>'Q'))/3+2+'0';
//else if(str1[j]>'Q'&&str1[j]<='Y')
//    str2[i][k]=(str1[j]-'A'-1)/3+2+'0';
else if(str1[j]>='0'&&str1[j]<='9')
str2[i][k]=str1[j];
k++;
}
}
}
qsort(str2,n+1,sizeof(str2[1]),cmp);
/*for(i=1;i<=n;i++)
puts(str2[i]);*/
f=0;
for(i=1;i<=n;i++)
{
num=1;
while(strcmp(str2[i],str2[i+1])==0)
{
num++;
i++;
}
if(num>=2)
{
printf("%c%c%c-%c%c%c%c %d\n",str2[i][0],str2[i][1],str2[i][2],str2[i][3],str2[i][4],str2[i][5],str2[i][6],num);
f=1;
}
}
if(f==0)printf("No duplicates.\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM POJ1002