The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.
The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word a comes earlier than word b in the dictionary if one of the following conditions hold:
- the first letter of a is less than the first letter of b;
- the first letters of a and b are the same, and the second letter of a is less than the second letter of bb.
So, the dictionary looks like that:
- Word 1:ab
- Word 2: ac
- ...
- Word 25: az
- Word 26: ba
- Word 27: bc
- ...
- Word 649: zx
- Word 650: zy
You are given a word s from the Berland language. Your task is to find its index in the dictionary.
Input
The first line contains one integer t (1≤t≤650) — the number of test cases.
Each test case consists of one line containing s — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
Output
For each test case, print one integer — the index of the word s in the dictionary.
Sample 1
Inputcopy | Outputcopy |
---|---|
7 ab ac az ba bc zx zy | 1 2 25 26 27 649 650 |
题意理解:可以把一个个字符串(由两个不同的字符组合而成)看成字典的一个个目录,问这个目录的位置(可以联想一下我们日常使用的字典目录的排列方式,每一位都是按照从a~z的顺序)
思路:以最后一个样例为例 S[0]==z可以表明前面肯定有25(因为z是第26个字母)组从a~z的排列,但是由于s[0]和s[1]不能相等,所以是25*25(相当于每一组大目录下面实际上是有25个小分支的,因为要排除例如aa,bb,cc等的情况);然后考虑最后一组数据,m是第几个字母就加几,如果m<n,就表明不存在S[0]和S[1]相等的情况,反之就要再减一,来排除这个数据。 例子:650=(26-1)*25+25(y<z,zz的情况不会被计算在内); 27=(2-1)*25+3-1(c>b,此时需要排除bb的情况)
注意:分类讨论
代码:
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
getchar();
string s;
cin >> s;
int n = s[0] - 'a' + 1;
int m = s[1] - 'a' + 1;
if (m < n)
cout << (n - 1) * 25 + m << endl;
else
cout << (n - 1) * 25 + m - 1 << endl;
}
return 0;
}
写的不太好,如果出现问题,欢迎批评指正啊