posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

PKU 1002 487-3279

Posted on 2007-06-03 21:54 ZelluX 阅读(1236) 评论(3)  编辑  收藏 所属分类: Algorithm
用了STL::map,很容易过了,前面用Java和朴素的C++都超时了
不过开始提交的时候题目没看清,没有重复的号码应该输出No duplicates.的
#include <iostream>
#include 
<string>
#include 
<map>

using namespace std;

string decode(const string& origin);

long main() {
    
long n;
    cin 
>> n;
    map
<stringlong> phones;
    
string origin;
    map
<stringlong>::iterator iter;
    
for (long i=0; i<n; i++{
        cin 
>> origin;
        
string decoded = decode(origin);
        iter 
= phones.find(decoded);
        
if (iter == phones.end()) {
            phones.insert(pair
<stringlong>(decoded, 1));
        }
 else {
            iter
->second++;
        }

    }


    
bool flag = true;
    
for (iter = phones.begin(); iter != phones.end(); iter++{
        
if (iter->second <= 1)
            
continue;
        cout 
<< iter->first.substr(03<< '-' << iter->first.substr(38)
             
<< ' ' << iter->second << endl;
        flag 
= false;
    }

    
if (flag)
        cout 
<< "No duplicates." << endl;
    
return 0;
}


string decode(const string& origin) {
    
string decoded;
    
for (long i = 0; i < origin.length(); i++{
        
switch (origin[i]) {
            
case 'A'case 'B'case 'C'case '2':
                decoded 
+= '2';
                
break;
            
case 'D'case 'E'case 'F'case '3':
                decoded 
+= '3';
                
break;
            
case 'G'case 'H'case 'I'case '4':
                decoded 
+= '4';
                
break;
            
case 'J'case 'K'case 'L'case '5':
                decoded 
+= '5';
                
break;
            
case 'M'case 'N'case 'O'case '6':
                decoded 
+= '6';
                
break;
            
case 'P'case 'R'case 'S'case '7':
                decoded 
+= '7';
                
break;
            
case 'T'case 'U'case 'V'case '8':
                decoded 
+= '8';
                
break;
            
case 'W'case 'X'case 'Y'case '9':
                decoded 
+= '9';
                
break;
            
case '1'case '0':
                decoded 
+= origin[i];
                
break;
        }

    }

    
return decoded;
}

评论

# re: PKU 1002 487-3279  回复  更多评论   

2007-09-16 15:00 by linux
haoxiang ye chaoshi le!

# re: PKU 1002 487-3279  回复  更多评论   

2007-09-16 19:17 by ZelluX
@linux
刚看了下以前的记录,发现是1795ms内过的,差点就tle了,好险@@

# re: PKU 1002 487-3279  回复  更多评论   

2008-02-21 00:45 by ecl1pse
我这个题也是1343MS过的 危险的很

只有注册用户登录后才能发表评论。


网站导航: