1 条题解

  • 1
    @ 2025-12-11 15:09:22

    放在字母表中看,只需要从单词开头的字母向单词结尾的字母移动,若移动中途经过了单词中存在的字母则加上该字母在单词中出现的次数。 统计单词出现的次数用哈希表即可。

    #include <iostream>
    #include <map>
    #include <cmath>
    using namespace std;
    int main()
    {
        unsigned T;
        cin >> T;
        while (T--)
        {
            string str;
            cin >> str;
            char start = str[0], target = str[str.size() - 1];
            map<char, int> dict;
            for (char c : str)
            {
                dict[c]++;
            }
            unsigned cost = abs(target - start);
            unsigned m = 0;
            if (target >= start)
            {
                for (char i = start; i <= target; i++)
                {
                    m += dict[i];
                }
            }
            else
            {
                for (char i = target; i <= start; i++)
                {
                    m += dict[i];
                }
            }
            cout << cost << " " << m << endl;
        }
        return 0;
    }
    
    • 1

    信息

    ID
    330
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    355
    已通过
    37
    上传者