ACM-ICPC 2018 徐州赛区网络预赛 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 22because 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 "1111 4545 1414".

       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≤10).

      For each test case:

      First line contains a integer NN and a character z, (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

解题思路

           由很多个两位数组成的,但是要去掉前导零。

           从第一个不为零的数开始计数。特殊情况: 当数全为零时,答案是1,不为0。

  举几个例子:

10 11 08 :答案为 6

01 00 08 :答案为 5

00 01 11 :答案为 3

00 00 00 :答案为 1 ,不为零

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
	int t,n,i,j,p,q,num;
	char z;
	string s;
	cin>>t;
	vector<int> v;
	for(i=0;i<t;i++)
	{
		cin>>n>>z>>s;
        num=0;
		for(j=0;j<n;j++)
		{
			p=abs((int)z-s[j]);
			if(p>=10)
            {
                q=p/10;
                v.push_back(q);
                num++;
                q=p%10;
                v.push_back(q);
                num++;
            }
            else
            {
                v.push_back(0);
                num++;
                v.push_back(p);
                num++;
            }
		}
		vector<int>::iterator it=v.begin();
		while(*it==0)
        {
            num--;
            it++;
			if(it==v.end())
			{
				num=1;
				break;
			}
        }
        cout<<num<<endl;
        v.clear();
	}
	return 0;
}

另解:

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
 
char a[1000000+10];
int b[1000000+10];
 
int main()
{
	int n,m,j,k,i,T,ans,start;
	char ch;
	cin>>T;
	while (T--)
	{
		ans=0;
		scanf("%d %c",&n,&ch);
		scanf("%s",a);
		for (i=0;i<n;i++)
		{
			if (abs((int)ch-a[i])!=0)
			{
				start = i;
				break;
			}
		}
		if (i>=n)
		{
			cout<<"1"<<endl;
			continue;
		}
		
		if (abs((int)ch-a[i])<=9 && abs((int)ch-a[i])>=1 )
		ans += 1;
		else
		ans += 2;
		
		for (i=start+1;i<n;i++)
		{
			ans+=2;
		}
		
		cout<<ans<<endl;
	}
	return 0;	
} 

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值