题目链接

题目描述
假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy 其中a的Index为0,aa的Index为1,aaa的Index为2,以此类推。 编写一个函数,输入是任意一个编码,输出这个编码对应的Index.

#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
char word[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char cod[15];
int stc=0,index_=0;
map<string,int> m;
int dfs(int ith);
int main()
{
    dfs(0);    
    string s;
    map<string,int> :: iterator it;
    /*
    for(it=m.begin();it!=m.end();it++)
    {
        cout<<it->first<<endl;
    }
    */
    while(cin>>s)
    {
        it=m.find(s);
        cout<<it->second<<endl;
    }
    return 0;
} 
int dfs(int ith)
{
    if(ith==4) return 0;
    for(int i=0;i<25;++i)
    {
        cod[stc]=word[i];    
        stc++;
        cod[stc]='\0';
        m[cod]=index_++;
        dfs(ith+1);
        stc--;
    }
    return 0;
}
Last modification:September 21st, 2019 at 12:20 am
如果觉得我的文章对你有用,请随意赞赏