A - 487-3279
Description
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
Output
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 以下为超时代码,太繁琐了!!!
#include <iostream>
#include <string>
using namespace std;
int f(char s[][],int x[],int y[],int n)
{
int q=,t,p,i,k,j;
for(i=;i<n-;i++){
if(s[i][]=='*')continue;
y[q]=;
for(k=i+;k<n;k++){
bool flag=true;
for(j=;j<;j++){
if(s[i][j]!=s[k][j]){
flag=false;
break;
}
}
if(flag==true){
s[k][]='*';
y[q]++;
}
}
if(y[q]>){
x[q]=i;
q++;
}
}
for(i=;i<q;i++){
for(j=;j<q-i-;j++)
for(p=;p<;p++){
if(s[x[j+]][p]>s[x[j]][p])break;
if(s[x[j+]][p]<s[x[j]][p]){
t=x[j];
x[j]=x[j+];
x[j+]=t;
t=y[j];
y[j]=y[j+];
y[j+]=t;
break;
}
}
}
return q;
}
int main()
{
int n,i,j,k=,x[],y[],q;
char s[][];
cin>>n;
while(k<n){
string str;
int a;
cin>>str;
a=str.length();
for(i=;i<a;i++){
if(str[i]=='-'){
for(j=i;j<a-;j++)str[j]=str[j+];
}
if(str[i]=='A'||str[i]=='B'||str[i]=='C')str[i]='';
if(str[i]=='D'||str[i]=='E'||str[i]=='F')str[i]='';
if(str[i]=='G'||str[i]=='H'||str[i]=='I')str[i]='';
if(str[i]=='J'||str[i]=='K'||str[i]=='L')str[i]='';
if(str[i]=='M'||str[i]=='N'||str[i]=='O')str[i]='';
if(str[i]=='P'||str[i]=='R'||str[i]=='S')str[i]='';
if(str[i]=='T'||str[i]=='U'||str[i]=='V')str[i]='';
if(str[i]=='W'||str[i]=='X'||str[i]=='Y')str[i]='';
if(i==){
str[i]='\0';
break;
}
}
for(i=;i<;i++)s[k][i]=str[i];
k++;
}
q=f(s,x,y,n);
for(i=;i<q;i++){
for(j=;j<;j++)cout<<s[x[i]][j];
cout<<'-';
for(j=;j<;j++)cout<<s[x[i]][j];
cout<<" "<<y[i]<<endl;
}
//system("pause");
return ;
}
不认真读题,n最大值为100000,上面定义s[100]!!!
修改几小时后!!
#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
int temp[];
int comp(const void *a,const void *b)
{
return *(int*)a-*(int*)b;
}
int change(string str)
{
int a,b;
a=str.length();
for(int i=;i<a;i++){
if(str[i]=='-'){
for(int j=i;j<a-;j++)str[j]=str[j+];
}
switch(str[i]){
case 'A':
case 'B':
case 'C':str[i]='';break;
case 'D':
case 'E':
case 'F':str[i]='';break;
case 'G':
case 'H':
case 'I':str[i]='';break;
case 'J':
case 'K':
case 'L':str[i]='';break;
case 'M':
case 'N':
case 'O':str[i]='';break;
case 'P':
case 'R':
case 'S':str[i]='';break;
case 'T':
case 'U':
case 'V':str[i]='';break;
case 'W':
case 'X':
case 'Y':str[i]='';break;
}
if(i==){
str[i]='\0';
break;
}
}
b=(str[]-'')*+(str[]-'')*+(str[]-'')*+(str[]-'')*+(str[]-'')*+(str[]-'')*+(str[]-'');
return b;
}
int main()
{
int n,k=,a=,b=;
bool flag=false;
cin>>n;
while(k<n){
string str;
cin>>str;
temp[k]=change(str);
k++;
}
qsort(temp, n, sizeof(int),comp);
for(int i=;i<n;++i){
if(i+<n&&temp[a]==temp[i+])b++;
else{
if(b){
flag=true;
printf("%03d-%04d %d\n", temp[a]/, temp[a]%, b+);
b=;
}
a=i+;
}
}
if(!flag) printf("No duplicates. \n");
// system("pause");
return ;
}
然后是WA!!!!!快被自己蠢死了!
对比了一下正确代码与自己代码发现,我的代码是对输入字符串中存在7个数字或大写字母,超过7个,之后的数字或大写字母不计算到电话号码中。而正确的代码将所有数字或大写字母都计算在内,因此
在我的代码中 111111111和1111111111111111111是相同的电话号码
而正确代码中,两者是不同的!!!
这就是题目的缺陷,题目提到 输入的字符串中只有7个,但测试时必然有多于七个的字符串,才导致我的代码的结果错误。至少我研究了很久,是这样想的。
此题要注意到字符串与数字的转换,这样才可以运用快速排序,避免程序超时
然后正确代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int telenum[];
int comp(const void *a,const void *b)
{
return *(int*)a-*(int*)b;
}
int main()
{ int i, j, n, cnt, len, flag, low, sum;
char temp[];
char alphalist[] = "";
scanf("%d", &n);
for(i=; i<n; ++i)
{
scanf("%s", temp);
len = strlen(temp);
sum = ;
for(j= ; j<len; ++j)
{
if('' <= temp[j] && '' >= temp[j])
sum = sum* + (temp[j] - '');
else if('A'<= temp[j] && 'Z' >= temp[j])
sum = sum* + (alphalist[temp[j]-'A']-'');
}
telenum[i] = sum;
}
qsort(telenum, n, sizeof(int),comp);
low = cnt = flag = ;
for(i=; i<n; ++i)
{
if(i+ < n && telenum[low] == telenum[i+])
cnt++;
else
{
if(cnt)
{
flag = ;
printf("%03d-%04d %d\n", telenum[low]/, telenum[low]%, cnt+);
cnt = ;
}
low = i+;
}
}
if(!flag) printf("No duplicates.\n");
//system("pause");
return ;
}
A - 487-3279的更多相关文章
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- CODEVS 3279 奶牛的健美操
3279 奶牛健美操 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...
- 【POJ 3279 Fliptile】开关问题,模拟
题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile[二进制状压DP]
题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...
- POJ 3279 - Fliptile - [状压+暴力枚举]
题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...
- POJ - 3279 Fliptile (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- poj 3279 Fliptile(二进制)
http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
- POJ 3279 Filptile dfs
题目链接:http://poj.org/problem?id=3279 大意:给出一块n*m的棋盘.里面放满了棋子.有1和0两种状态.给出初始状态,翻动的时候会把当前位置和当前位置的上下左右共五个位置 ...
随机推荐
- iOS-设计模式之代理反向传值
代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...
- ios开发中的基本设计模式
(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过和protoc ...
- (转) 学习C++ -> 向量(vector)
vector是向量类型,它是一种对象实体,具有值,所以可以看作是变量. 它可以容纳许多其他类型的相同实体,如若干个整数,所以称其为容器. vector类与一般的Array类的区别在于: 1 ...
- Python初学
经同学推荐,学习了下Python语言,看Python的介绍,它本身是一个面向对象的解释型脚本语言,我初看到这句话的时候就在想,一个脚本语言还搞成面向对象?有这个必要么?原谅我肤浅了一把. 它还被俗称为 ...
- Hibernate之通过hibernate.cfg.xml配置文件访问数据库的例子
hibernate.cfg.xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- discuz x2 个人资料项排序问题解决方法、添加自定义字段、修改栏目名称和介绍
第一次写文章,希望与人提供方便同时,别误人子弟,自己研究的,大家看不懂只改文件就可以了,如果发现不对的地方请回复或直接通知我,谢谢,本来想在discuz论坛上发的,不懂版规也没时间看版规,怕发错,隔小 ...
- [Head First Python]6. 定制数据对象:打包代码与数据
相同功能,演进实现 数据文件 sarah2.txt sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55 1- 返回 ...
- NAS4Free 配置BT下载
NAS4Free 9.3.0.2 开启BT下载功能 Services|BitTorrent 选中右上角的复选框 Peer port 是监听端口,用于接受外部连接,需要在路由器配置该端口到服务器,才能提 ...
- C# Environment类_获取程序所在机器信息
一.属性 CommandLine 获取该进程的命令行.CurrentDirectory 获取或设置当前工作目录的完全限定路径.ExitCode 获取或设置进程的退出代码.HasShutdownSta ...
- JavaScript 常用小代码
//判断一个汉字等于两个字符 function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { var ...