随笔-141  评论-9  文章-3  trackbacks-0

今天开始做网上一份字符串匹配专题。

这题当热身啦。

题意:规定号码的格式是 XXX-XXXX,

符号转换为数字的映射如下:

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.


有一些字符表示,按序显示重复的号码和重复的次数。

#include <iostream>
#include 
<map>
#include 
<string>

using namespace std;

const int MAX = 20;

char hash[]="22233344455566677778889999";

int main(){
    
int i, j, n;
    
char ch[MAX],buf[MAX];

    
//freopen("input.txt", "r", stdin);

    map
<stringint> dir;

    scanf(
"%d"&n);

    
for(i=0; i<n; ++i){
        scanf(
"%s", ch);
        
int len = strlen(ch), t=0;
        memset(buf, 
0sizeof(buf));
        
for(j=0; j<len; ++j){
            
if(ch[j]=='-'
                
continue;
            
if(ch[j]<='9' && ch[j]>='0')
                buf[t
++]=ch[j];
            
else
                buf[t
++]=hash[ch[j]-'A'];
            
if(t==3)
                buf[t
++]='-';
        }

        buf[t]
='\0';

        dir[
string(buf)]++;
    }


    
bool flag =false;
    
for(map<string,int>::iterator iter = dir.begin(); iter!=dir.end(); ++iter){
        
if(iter->second!=1){
            flag
=true;
            cout
<<iter->first<<" "<<iter->second<<endl;
        }

    }


    
if(!flag)
        cout
<<"No duplicates." << endl;    

//    system("pause");

    
return 0;
}

 

posted on 2011-03-15 20:34 小阮 阅读(151) 评论(0)  编辑 收藏 引用 所属分类: POJ

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理