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 2because 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 TT , the number of test cases (T \le 10)(T≤10).

For each test case:

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

Second line contains a string with length NN . 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 徐州赛区网络预赛

 

这道题如果英语水平不太好的话可能就不能完全理解题意(我就是),尤其是一会保留前导零,一会不保留前导零,我当时就被它给弄晕啦,后来问了大神才明白了题意,他的意思就是对字符串中的每个字符从前往后按题中的公式运算,他说保留前导零,并且公式结果长度为2,这样的货就是说如果计算结果为1的话那么应该写成01,最后再将所有的结果合并起来,而合并起来的这个结果是不保留前导零的,比如三个结果分别是00 10 12 那么合并后的结果是1012为4为;

 

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<stdio.h>
using namespace std;
struct Store {//a,b代表每一个结果的两位数是不是为零,false为零,true不为零
	bool a=false;
	bool b=false;
}store[10000001];
int main()
{
	
	//freopen("own.txt", "r", stdin);
	int t;
	cin >> t;
	while (t--) {
		int n;
		char z;
		bool flag =false;
		cin >> n >> z;
		string str;
		cin >> str;
		for (int i = 0; i < n; i++) {
			int x = abs((int)z - str[i]);
			if (x >= 10) {//如果运算的结果为两位数,那么肯定是ab都为true
				store[i].a = true;
				store[i].b = true;
			}
			else if (x > 0 && x <= 9) {//如果是一位数,那么就是a假b真
				store[i].a = false;
				store[i].b = true;
			}
			else if (x == 0) {//等于零就都是假
				store[i].a = false;
				store[i].b = false;
			}
	}
		int ans = 0;
		for (int i = 0; i < n; i++) {
			if (flag) {//flag用来辅助判断有没有前导零,如果没有的话,以后的数就可以放心的加2啦
				ans += 2;
			}
			else {
				if (store[i].a == true) {//下边两个if语句是从最后合并的结果中找到第一个不是false的地方并计数
					ans++;
					flag = true;
				}
			if (store[i].b == true) {
				ans++;
				flag = true;
			}
			}

		}
		if (ans == 0)
			cout << 1 << endl;
		else
		cout << ans << endl;
	}
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值