计蒜客 ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash

Description:

Mur loves hash algorithm, and he sometimes encrypt another one’s name, and call him with that encrypted value. For instance, he calls Kimura KMR, and calls Suzuki YJSNPI. One day he read a book about SHA- 256 256 256 , which can transit a string into just 256 256 256 bits. Mur thought that is really cool, and he came up with a new algorithm to do the similar work. The algorithm works this way: first we choose a single letter L as the seed, and for the input(you can regard the input as a string s s s, s [ i ] s[i] s[i] represents the i i ith character in the string) we calculates the value( ( i n t ) L s [ i ] |(int) L - s[i]| (int)Ls[i]), and write down the number(keeping leading zero. The length of each answer equals to 2 2 2 because the string only contains letters and numbers). Numbers writes from left to right, finally transfer all digits into a single integer(without leading zero( s s s)). For instance, if we choose ‘z’ as the seed, the string “oMl” becomes " 11 11 11 45 45 45 14 14 14".It’s easy to find out that the algorithm cannot transfer any input string into the same length. Though in despair, Mur still wants to know the length of the answer the algorithm produces. Due to the silliness of Mur, he can even not figure out this, so you are assigned with the work to calculate the answer.

Input:

First line a integer T T T , the number of test cases ( T 10 ) (T \le 10) (T10).

For each test case:

First line contains a integer N N N and a character z z z, ( N 1000000 ) (N \le 1000000) (N1000000).

Second line contains a string with length N N N . Problem makes sure that all characters referred in the problem are only letters.

Output:

A single number which gives the answer.

Sample Input:

2
3 z
oMl
6 Y
YJSNPI

Sample Output:

6
10

题目链接

给一个目标字符,用目标字符减字符串中所有字符的绝对值按顺序拼凑为一个数,求此数位数。

  1. 去掉前导0(包含第一个有效数字为一位数的第一个0)
  2. 若拼凑的数为0则0是一位数

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]) {
    int T;
    cin >> T;
    for (int Case = 1, N; Case <= T; ++Case) {
        char Seed;
        string str;
        cin >> N >> Seed;
        if (N == 0) {
            cout << 0 << endl;
            continue;
        }
        getchar();
        getline(cin, str);
        int Ans = 0;
        bool Flag = false;
        int Cnt = 0;
        for (int i = 0; i < int(str.length()); ++i) {
            if (str[i] != Seed) {
                Flag = true;
            }
            if (Flag) {
                if (Cnt == 0) {
                    Cnt = abs(Seed - str[i]);
                    if (Cnt < 10) {
                        Ans += 1;
                    }
                    else {
                        Ans += 2;
                    }
                }
                else {
                    Ans += 2;
                }
            }
        }
        Ans = Ans == 0 ? 1 : Ans;
        cout << Ans << endl;
    }
    return 0;
}
全部评论

相关推荐

有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务