pku 1002题

djjlove_2008 2010-04-21 03:34:28
487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 126740 Accepted: 21605

Description

企业喜欢用容易被记住的电话号码。让电话号码容易被记住的一个办法是将它写成一个容易记住的单词或者短语。例如,你需要给滑铁卢大学打电话时,可以拨打TUT-GLOP。有时,只将电话号码中部分数字拼写成单词。当你晚上回到酒店,可以通过拨打310-GINO来向Gino's订一份pizza。让电话号码容易被记住的另一个办法是以一种好记的方式对号码的数字进行分组。通过拨打必胜客的“三个十”号码3-10-10-10,你可以从他们那里订pizza。

电话号码的标准格式是七位十进制数,并在第三、第四位数字之间有一个连接符。电话拨号盘提供了从字母到数字的映射,映射关系如下:
A, B, 和C 映射到 2
D, E, 和F 映射到 3
G, H, 和I 映射到 4
J, K, 和L 映射到 5
M, N, 和O 映射到 6
P, R, 和S 映射到 7
T, U, 和V 映射到 8
W, X, 和Y 映射到 9

Q和Z没有映射到任何数字,连字符不需要拨号,可以任意添加和删除。 TUT-GLOP的标准格式是888-4567,310-GINO的标准格式是310-4466,3-10-10-10的标准格式是310-1010。

如果两个号码有相同的标准格式,那么他们就是等同的(相同的拨号)

你的公司正在为本地的公司编写一个电话号码薄。作为质量控制的一部分,你想要检查是否有两个和多个公司拥有相同的电话号码。

Input

输入的格式是,第一行是一个正整数,指定电话号码薄中号码的数量(最多100000)。余下的每行是一个电话号码。每个电话号码由数字,大写字母(除了Q和Z)以及连接符组成。每个电话号码中只会刚好有7个数字或者字母。
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
Source

East Central North America 1999
Translator

北京大学程序设计实习2007
我这样写得:怎么有编译错误了?
#include<stdio.h>
int list[10000000]={0};
int res[100001];
int resNum=0,n;
const char word[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9};

int reader()
{
char ch[16];
int i,t,res=0;
scanf("%s",ch);
for(i=0;ch[i];i++)
if(ch[i]!='-')
{
if(ch[i]<'A')
t=ch[i]-48;
else
t=word[ch[i]-'A'];
res=10*res+t;
}
return(res);
}

void show(int num)
{
int t=num;
printf("%d",num/1000000);num%=1000000;
printf("%d",num/100000);num%=100000;
printf("%d",num/10000);num%=10000;
printf("-");
printf("%d",num/1000);num%=1000;
printf("%d",num/100);num%=100;
printf("%d",num/10);num%=10;
printf("%d %d",num,list[t]);
printf("\n");
}

inline void swap(int &a,int &b)
{
int t=a;
a=b;b=t;
}

void Quick(int A[],int left,int right)
{
int i=left,j=right,mid=A[(left+right)/2];
do
{
while(A[i]<mid)
i++;
while(A[j]>mid)
j--;
if(i>=j)
break;
swap(A[i++],A[j--]);
}while(i<=j);
if(left<i-1)
Quick(A,left,i-1);
if(j+1<right)
Quick(A,j+1,right);
}

int main()
{
int i,t;
scanf("%d",&n);

for(i=1;i<=n;i++)
{
t=reader();
if(list[t]==1) //玄妙
{
resNum++;
res[resNum]=t;
}
list[t]++;
}

if(resNum>0)
{
Quick(res,1,resNum);
for(i=1;i<=resNum;i++)
show(res[i]);
}

else
{
printf("No duplicates.\n");
}


return 0;
}
...全文
289 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
djjlove_2008 2010-04-25
  • 打赏
  • 举报
回复
怎么没人了,我自己踩踩。。。呵呵
djjlove_2008 2010-04-24
  • 打赏
  • 举报
回复
怎么没人,麻类各人。。。
djjlove_2008 2010-04-23
  • 打赏
  • 举报
回复
效率,谢谢。。。谢谢。。。
dskit 2010-04-22
  • 打赏
  • 举报
回复
trie 树的版本,效率较高:

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

#ifdef DEBUG
#include<string>
using std::string;
using std::freopen;
string input_file_name = __FILE__;
#endif

//here add other file need to included, and declare namespace need to use.
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
//here declare global variables;
char dictionary[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};
char no[50];
char oarray[10];
bool flag;

struct Trie_node
{
int count;
Trie_node* next[10];
};

Trie_node root;

Trie_node node_pool[200002];
int index;

void inline insert(Trie_node* node, char* s)
{
if(*s == '\0')
{
++node->count;
return;
}

if(node->next[*s - '0'] == NULL)
node->next[*s - '0'] = &node_pool[index++];

insert(node->next[*s - '0'], s + 1);
}

void inline travel(Trie_node* node, int c)
{
for(int i = 0; i < 10; ++i)
{

if(node->next[i] == NULL)
continue;

oarray[c] = (char)(i + '0');

if(c >= 6)
{
if(node->next[i]->count > 1)
{
flag = true;
printf("%c%c%c-%c%c%c%c %d\n", oarray[0], oarray[1],
oarray[2], oarray[3],
oarray[4], oarray[5],
oarray[6], node->next[i]->count);
}
}
else
{
travel(node->next[i], c + 1);
}
}

}


//here add funtions.
void inline format_number(char* no)
{
//int len = strlen(no);
//printf("%s\n", no);
int count = 0;
int sum = 0;
for(int i = 0; no[i] != '\0'; ++i)
{
++sum;
if(no[i] >= 'A' && no[i] <= 'Z')
{
no[i] = '0' + dictionary[no[i] - 'A'];
}
else if(no[i] == '-')
{
++count;
}

if(no[i] != '-')
no[i - count] = no[i];

}
no[sum - count] = '\0';
//printf("%s\n", no);
}

int main(int argc, char* argv[])
{
#ifdef DEBUG
if(!freopen((input_file_name.substr(0, input_file_name.size() - 4) + ".in").c_str(), "r", stdin))
{ cout << "input data error, not found input file." << endl; return -1; }
#endif

//here add code for solve problem.
int n = 0;
//input
scanf("%d", &n);
flag = false;
index = 0;
getchar();
for(int i = 0; i < n; ++i)
{
gets(no);
//puts(no);
format_number(no);
insert(&root, no);
}

travel(&root, 0);

if(!flag)
{
printf("No duplicates.\n");
}

return 0;
}



dskit 2010-04-22
  • 打赏
  • 举报
回复

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

#ifdef DEBUG
#include<string>
using std::string;
using std::freopen;
string input_file_name = __FILE__;
#endif

//here add other file need to included, and declare namespace need to use.
#include<stdlib.h>
//here declare global variables;
char dictionary[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};
char nos[100002][50];

//here add funtions.
void format_number(char* no)
{
int len = strlen(no);
//printf("%s\n", no);
int count = 0;
for(int i = 0; i < len; ++i)
{
if(no[i] >= 'A' && no[i] <= 'Z')
{
no[i] = '0' + dictionary[no[i] - 'A'];
}
else if(no[i] == '-')
{
++count;
}

if(no[i] != '-')
no[i - count] = no[i];

}
no[len - count] = '\0';
//printf("%s\n", no);
}

int cmp(const void* a, const void* b)
{
return strcmp((char*)a, (char*)b);
}

int main(int argc, char* argv[])
{
#ifdef DEBUG
if(!freopen((input_file_name.substr(0, input_file_name.size() - 4) + ".in").c_str(), "r", stdin))
{ cout << "input data error, not found input file." << endl; return -1; }
#endif

//here add code for solve problem.
int n = 0;
//input
scanf("%d", &n);

for(int i = 0; i <= n; ++i)
{
gets(nos[i]);
format_number(nos[i]);
}

qsort(nos, n + 1, 50, cmp);

//output
int count = 0;
bool flag = false;
for(int i = 0; i <= n; ++i)
{
if(strcmp(nos[i], nos[i + 1]) == 0)
++count;
else
{
if(count > 0)
{
printf("%c%c%c-%c%c%c%c %d\n", nos[i - 1][0], nos[i - 1][1],
nos[i - 1][2], nos[i - 1][3],
nos[i - 1][4], nos[i - 1][5],
nos[i - 1][6], count + 1);
flag = true;
}
count = 0;
}

}

if(!flag)
{
printf("No duplicates.\n");
}

return 0;
}


djjlove_2008 2010-04-22
  • 打赏
  • 举报
回复
哎,谢谢。。。
ithiker 2010-04-21
  • 打赏
  • 举报
回复
帮顶....
logiciel 2010-04-21
  • 打赏
  • 举报
回复
LZ程序按C++编译没错,但按C编译有错.
poppoo1986 2010-04-21
  • 打赏
  • 举报
回复
看了,发现超出我的知识范围

帮顶

64,666

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧