在竞赛中,STL提供了极为强大的模板工具。熟练掌握能使编码复杂度大大降低,同时出错率也减少许多。
map,映像,映射,是一种比较常见的关联容器。其存储这键-值对,对其访问时下表与常用数组访问有所区别,map<char,char> cal,定义了一个cal容器,对其访问时下表为char字符,这点在很多运算中非常方便,可以免去繁琐的 if。。。。else。。或者 switch。。。case。。。。
最常用的是在数据结构中,或者是在上下文无关词法分析时,判断优先级异常迅速哇。
触发我写下这篇罗里吧嗦文章的×××是,HDU2816.。。
题目叙述很长,中间转换步骤异常多,有兴趣的读者可以自己去看看题意:
http://acm.hdu.edu.cn/showproblem.php?pid=2816
下面给出我的源码(一次就AC了):
Author:jeick(贾钰),College of ComputerScience,Sichuan University
Time:2011-5-23 00:10
Code:
#include<cstring>
#include<map>
#include<iostream>
using namespace std;
const int Max = 505;
char AVE[2*Max];
char _CHAR[Max];
char RSLT[Max];
int _index;
char LETTER[9][4]={{'_','\0','\0','\0'}, //手机按键与相对应字母
{'A','B','C','\0'},
{'D','E','F','\0'},
{'G','H','I','\0'},
{'J','K','L','\0'},
{'M','N','O','\0'},
{'P','Q','R','S'},
{'T','U','V','\0'},
{'W','X','Y','Z'}
};
map<char,char> Project; //键盘字母序列与字典序列的映射
void map_init()
{
Project.insert(make_pair('Q','A'));Project.insert(make_pair('W','B'));
Project.insert(make_pair('E','C'));Project.insert(make_pair('R','D'));
Project.insert(make_pair('T','E'));Project.insert(make_pair('Y','F'));
Project.insert(make_pair('U','G'));Project.insert(make_pair('I','H'));
Project.insert(make_pair('O','I'));Project.insert(make_pair('P','J'));
Project.insert(make_pair('A','K'));Project.insert(make_pair('S','L'));
Project.insert(make_pair('D','M'));Project.insert(make_pair('F','N'));
Project.insert(make_pair('G','O'));Project.insert(make_pair('H','P'));
Project.insert(make_pair('J','Q'));Project.insert(make_pair('K','R'));
Project.insert(make_pair('L','S'));Project.insert(make_pair('Z','T'));
Project.insert(make_pair('X','U'));Project.insert(make_pair('C','V'));
Project.insert(make_pair('V','W'));Project.insert(make_pair('B','X'));
Project.insert(make_pair('N','Y'));Project.insert(make_pair('M','Z')); //字符映射表建立完成
}
void Trans() //具体的映射、转换过程
{
int len = strlen(AVE);
_index = len/2;
for(int i=0;i<_index;i++) //把数字转换成字符,然后再从字符转换成键盘映射字符
{
int temp1 = AVE[2*i]-'0'-1;
int temp2 = AVE[2*i+1]-'0'-1;
char letter = LETTER[temp1][temp2];
_CHAR[i] = Project[letter];
}
int pnd = (_index+1)/2;
int Ick =0;
for(int i=0;i<pnd-1;i++)
{
RSLT[Ick++] = _CHAR[i];
RSLT[Ick++] = _CHAR[pnd+i];
}
RSLT[Ick++] = _CHAR[pnd-1];
if(2*pnd==_index) //如果恰好有证书个字符
{
RSLT[Ick++] = _CHAR[_index-1];
}
for(int j=Ick-1;j>=0;j--)
cout<<RSLT[j];
cout<<endl;
}
int main()
{
map_init();
while(cin >> AVE)
{
Trans();
}
return 0;
}
_________________________________________________________________________
补充:在回文时 STL提供了一个函数 reverse(char* _char);
这里没有使用这个函数,使用的话无非增加了时间复杂度。
Connecting me:jeick89@gmail.com
转载于:https://blog.51cto.com/jeick/551644