I. Characters with Hash【ACM-ICPC 2018 徐州赛区网络预赛】

原题连接:I. Characters with Hash

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-256256 , which can transit a string into just 256256 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 ss, s[i]s[i] represents the iith character in the string) we calculates the value(|(int) L - s[i]|∣(int)L−s[i]∣), and write down the number(keeping leading zero. The length of each answer equals to 22 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(ss)). For instance, if we choose 'z' as the seed, the string "oMl" becomes "11 45 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 , the number of test cases (T \le 10)(T≤10).

For each test case:

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

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

Output

A single number which gives the answer.

样例输入复制

2
3 z
oMl
6 Y
YJSNPI

样例输出复制

6
10

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

题记:

先说下这道题的意思:输入长度为n的字符串s和一个字符z,依次计算z和s[i]的差值,得到一串数字,输出数字的位数(前面的零不算,中间的算)。

再解释解释题中的例子

1.输入长度为3的字符串oML和字符z,分别计算“z-o” 、"z-M"、"z-L"的绝对值,得到 “11 45 14”,前面没有0,长度为6,输出6。

2.输入长度为6的字符串YJSNPI和字符Y,分别计算“Y-Y”、"Y-J"、"Y-S"、"Y-N"、"Y-P"、"Y-I"的绝对值,得到“00 15 06 11 09 16”,前面的0不算,长度为10,输出10。

最后说说我的思路:从s[0]开始计算,如果z-s[0]=0,就继续往下,直到找到z-s[i] != 0,就不计算了,位数val=2*(n-i),因为后面每一位相减都按两位数算,所以直接计算2乘后面的字母数量,需要注意在考虑下第i位,就是停止计算的那位,是一位还是两位,这也是代码中写的“if(abs(z-s[i])<10),val-- ”的意思。

ps:队友说我思路新奇。。希望我这样表达有人懂……

下面给了两个程序,思路一样,如上文所说,代码实现上有小区别。朋友们可以挑个好理解的看……

C++程序如下:

#include <iostream>
#include <string.h>
#include <cmath>

using namespace std;
const int N=1000000;

int main(void)
{
	int t;
	
	cin >> t;
	while(t--){
		char z, s[N];
		int n, num, val=0;
		
		cin >> n >> z;
		for(int i=0; i<n; i++)
		    cin >> s[i];
		for(int i=0; i<n; i++){
			num = 0;
			num = abs(z-s[i]);
			if(num != 0){
				val = (n-i)*2;
				if(num<10)
				    val--;
				break;
			}
		}
		cout << val << endl;
	}
	return 0;
}
#include <iostream>
#include <string.h>
#include <cmath>
 
using namespace std;
const int N=1000000;
 
int main()
{
    int t;
    char z;
    int n;
 
    string s;
    cin >> t;
    while(t--){
        cin >> n >> z;
        int val=0;
 
         cin >> s;
         for(int i=0; i<n; i++){
 
             if(abs(z-s[i])){
                 val = (n-i)*2;
                 if(abs(z-s[i])<10)
                     val--;
                 break;
             }
         }
         if(val)
         cout << val << endl;
         else cout << "1" << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fyy_lufan/article/details/82560085