您的位置:首页 > 其它

UVA 417 - Word Index

2012-10-25 13:18 393 查看
题目大意:a=1,b=2……z=26,ab=27……,前面的字母一定会比后面大,比如ab,ac,不能是ca等

思路分析:数据很小最大只到VWXYZ,可以暴力,第一次用map

AC code:

#include<iostream>

#include<cstdlib>

#include<cstring>

#include<cstdio>

#include<map>

#include<string>

#include<algorithm>

using namespace std;

int main()

{

map<string,int> word;

int cnt=1;

char temp[6];

for(char i=\'a\'; i<=\'z\'; i++)

{

temp[0]=i;

word[(string)temp]=cnt++;

}

for(char i=\'a\'; i<=\'y\'; i++)

{

temp[0]=i;

for(char j=i+1; j<=\'z\'; j++)

{

temp[1]=j,temp[2]=\'\\0\';

word[(string)temp]=cnt++;

}

}

for(char i=\'a\'; i<=\'x\'; i++)

{

temp[0]=i;

for(char j=i+1; j<=\'y\'; j++)

{

temp[1]=j;

for(char k=j+1; k<=\'z\'; k++)

{

temp[2]=k,temp[3]=\'\\0\';

word[(string)temp]=cnt++;

}

}

}

for(char i=\'a\'; i<=\'w\'; i++)

{

temp[0]=i;

for(char j=i+1; j<=\'x\'; j++)

{

temp[1]=j;

for(char k=j+1; k<=\'y\'; k++)

{

temp[2]=k;

for(char l=k+1; l<=\'z\'; l++)

{

temp[3]=l,temp[4]=\'\\0\';

word[(string)temp]=cnt++;

}

}

}

}

for(char i=\'a\'; i<=\'v\'; i++)

{

temp[0]=i;

for(char j=i+1; j<=\'w\'; j++)

{

temp[1]=j;

for(char k=j+1; k<=\'x\'; k++)

{

temp[2]=k;

for(char l=k+1; l<=\'y\'; l++)

{

temp[3]=l;

for(int m=l+1; m<=\'z\'; m++)

{

temp[4]=m,temp[5]=\'\\0\';

word[(string)temp]=cnt++;

}

}

}

}

}

string s;

while(cin>>s)

cout<<word[s]<<endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: