练习cf1674B. Dictionary

题目如下
B. Dictionary
time limit per test2 seconds
memory limit per test512 megabytes
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 𝑎 comes earlier than word 𝑏 in the dictionary if one of the following conditions hold:

the first letter of 𝑎 is less than the first letter of 𝑏;
the first letters of 𝑎 and 𝑏 are the same, and the second letter of 𝑎 is less than the second letter of 𝑏.
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 𝑠 from the Berland language. Your task is to find its index in the dictionary.

Input
The first line contains one integer 𝑡 (1≤𝑡≤650) — the number of test cases.

Each test case consists of one line containing 𝑠 — 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 𝑠 in the dictionary.

题目大意
现有Berland语单词,每个都由两个不同小写字母组成,根据Berland词典找出他们在词典中的索引。
Berland词典中,词典根据一下规则排序:
满足任意一条就确定单词a索引小于b。
1.a的第一个字母小于b的第一个字母;
2.a的第一个字母与bd第一个字母相同,此时a的第二个字母小于b的第二个字母。

题目分析
先令第一个字母与a比较,因为每个字母开头的单词都有25个,差值乘以25,再通过与a[0]与a[1]的比较,计算对应开头下的位置。

点击查看代码
#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    while(n--){
        string s;
        cin >> s;
        int index = 0;
        index = (s[0] - 'a') * 25;
        if(s[1] < s[0]){
            index += s[1] - 'a';
        }else{
            index += s[1] - 'a' - 1;
        }
        cout << index + 1 << endl;
    }
    return 0;
}
posted @ 2025-07-23 21:32  sirro1uta  阅读(10)  评论(0)    收藏  举报