Dictionary

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

InputcopyOutputcopy
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;
}

写的不太好,如果出现问题,欢迎批评指正啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值