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 aa comes earlier than word bb in the dictionary if one of the following conditions hold:
- the first letter of aa is less than the first letter of bb;
- the first letters of aa and bb are the same, and the second letter of aa is less than the second letter of bb.
So, the dictionary looks like that:
- Word 11: ab
- Word 22: ac
- ...
- Word 2525: az
- Word 2626: ba
- Word 2727: bc
- ...
- Word 649649: zx
- Word 650650: zy
You are given a word ss from the Berland language. Your task is to find its index in the dictionary.
Input
The first line contains one integer tt (1 \le t \le 6501≤t≤650) — the number of test cases.
Each test case consists of one line containing ss — 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 ss in the dictionary.
Sample 1
Inputcopy | Outputcopy |
---|---|
7 ab ac az ba bc zx zy | 1 2 25 26 27 649 650 |
伯兰语由只有两个字母的单词组成。而且,单词的第一个字母和第二个字母是不同的。两个不同的伯兰字母的任何组合(顺便说一下,它们与拉丁字母的小写字母相同)在伯兰语言中都是正确的单词。
伯兰字典包含这种语言的所有单词。单词是按照字典中通常的顺序排列的。形式上,如果满足以下条件之一,则字典中单词aa比单词bb更早出现:
aa的首字母小于bb的首字母;
aa和bb的首字母相同,且aa的第二个字母小于bb的第二个字母。
字典是这样的:
- Word 1: ab
- Word 2: ac
- ...
- Word 25: az
- Word 26: ba
- Word 27: bc
- ...
- Word 649: zx
- Word 650: zy
你会得到一个来自伯兰语的单词s。你的任务是在字典里找到它的索引。
输入
第一行包含一个整数t (1≤t≤650)-测试用例的数量。
每个测试用例由一行包含s的内容组成——这是一个完全由两个不同的小写拉丁字母组成的字符串。伯兰语中的一个正确的词。
输出
对于每个测试用例,打印一个整数—单词s在字典中的索引。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
char a[30],b[30];
int main() {
int n;
cin>>n;
while(n--) {
string s;
cin>>s;
a[0]=s[0],b[0]=s[1];
if(a[0]=='a')
cout<<(b[0]-a[0])<<endl;
else {
if(a[0]>b[0]) cout<<(a[0]-'a')*25+(b[0]-'a'+1)<<endl;
if(a[0]<b[0]) cout<<(a[0]-'a')*25+(b[0]-'a')<<endl;
}
}
return 0;
}
这道题其实非常简单,首先我们定义一个字符串(这个字符串就是伯兰语的一个字,有两个不相同的小写字母组成),令字符串的第一个字符等于a[0],第二个字符等于b[0]。
如果字符串的第一个字符为‘a',那么这个字符串在字典中的索引为b[0]-a[0](到这里我相信一些新手就会不理解了,b[0]和a[0]不是字符吗,怎么相减啊,其实这里的相减指的是字符的ASCLL码值相减),同时因为第一个字符为'a',所以在字典中不用考虑两个字符相等时的情景,在字典中直接是从ab开始到az中间第二个字符是按照二十六个字母的顺序排列的,没有间断,所以可以直接用第二个字符的ASCLL码值减去第一个字符的ASCLL码值就可以得到这个字符串的索引了。
如果字符串的第一个字符不为'a',这个时候就要分情况讨论了,当第一个字符的ASCLL码值大于第二个字符的ASCLL码值时(即第一个字符在26个字母表中的位置在第二个字符后面),用第一个字符的ASCLL码值减去字符'a'的ASCLL码值然后乘以25算出这个字符串索引的大致范围(因为以相同字符作为第一个字符的情况共有25种,用第一个字符减去‘a'是为了算出相差几个字符)再加上第二个字符减去字符'a'再加1;当第一个字符小于第二个字符时,和第一种情况几乎相同但是最后不用加1(因为第一个字符在26个字母表中的位置在第二个字符前面).
第一个字符 | 索引 | 第一个字符减'a' |
a | 1-25 | 0 |
b | 26-50 | 1 |
c | 51-75 | 2 |
... | ... | ... |
y | 601-625 | 24 |
z | 626-650 | 25 |
让我们几个例子来验证一下
- af 索引为f-a=5;
- zb 索引为(z-a)*25=625,625+b-a+1=627;
- cz 索引为(c-a)*25=50,50+z-a=75;