題目鏈接:題目
#include<iostream>
using namespace std;
/**************** 分析 ******************/
// 第一步,在草稿紙上畫出編碼的字典順序表
// 第二步,觀察規律
// 第三步,思考策略:
// 由大到小,先確定大範圍,再確定小範圍,即:
// 先根據第一位字符確定前面有多少個元素,再根據第二位字符確定前面有多少個元素,再第三位,再第四位
int main(int argc, char* argv[])
{
/***************** Data /*****************/
char s[102];
int P[5] = {0}; // P[0]表示由於第0位(即第一位)加一,所帶來的 Index 的增加量
/***************** Input /*****************/
cin >> s;
/***************** Process /*****************/
P[0] = (26 * 25 + 1) * 25 + 1;
P[1] = 26 * 25 + 1;
P[2] = 26;
P[3] = 1;
/***************** Output /*****************/
int index = 0;
for (int i = 0; i < strlen(s); i++){
index += (s[i] - 'a')*P[i];
if (i>0) index++;
}
cout << index << endl;
return 0;
}