ACM-ICPC 2018 徐州賽區網路預賽 I. Characters with Hash【簽到題】

Enjoy_process發表於2018-09-09
  •  1000ms
  •  262144K

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 , which can transit a string into just 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[i] represents the iith character in the string) we calculates the value(∣(int)L−s[i]∣), and write down the number(keeping leading zero. The length of each answer equals to 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)). 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 (T≤10).

For each test case:

First line contains a integer N and a character z, N (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 徐州賽區網路預賽

題目大意:T組測試,每組給出整數n和字母L,以及一個長度為n的字串S(只含字母),字母L減去S的每一個字元後取絕對值,結果不足2位的補前導零,然後拼湊成一個字串,問這個此字串去掉前導零後的長度。比如L='z' ,S='oMl',那麼拼湊的字串為“11 45 14”,長度為6

題解:由於不夠兩位的要補前導零,因此實際長度就為2*n,但是這是沒有去除前導零的長度,正確答案是要去掉前導零,只要去掉前導零即可,但是要注意當結果全是0時,去掉前導零後就保留一個0,因此答案為1

AC的C++程式碼:

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

int main()
{
	int t,length,ans;
	char c;
	string s;
	scanf("%d",&t);
	while(t--){
		scanf("%d %c",&length,&c);
		ans=length*2;
		cin>>s ;
		for(int i=0;i<length;i++){
			int temp=abs(c-s[i]);
			if(!temp) ans-=2;//如果是前導零 就要減去2個0 
			else{
				if(temp<10)//如果第一個非零的數小於10就要去掉1個前導0 
				  ans-=1;
				break;
			}
		}
	   if(ans==0)//當結果全是0時,去掉前導零後就保留一個0,因此答案為1
	     ans=1;
       cout << ans <<endl;
	}
	
	return 0;
} 

 

相關文章