2021-01-29

今天学习了有关hash入门的内容。
hash思想指的是建立一个以key值作为映射的散列的函数,把值存储到内存中的一个位置来访问的算法思想。
hash思想的优越性在于遍历数据的速度较普通的遍历算法要来的快的多,因为它把数据有序的放在了规定好的存储格子中,检索起来就方便的多。
要注意的是放置的格子要考虑避免哈希冲突。
下面放到例题。

Consider equations having the following form:

ax12+b*x22+cx32+d*x42=0

a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.

Determine how many solutions satisfy the given equation.

Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.

Output
For each test case, output a single line containing the number of the solutions.

Sample Input
1 2 3 -4
1 1 1 1

Sample Output
39088
0

这题的思路就是把解存放到对应的两个区间中,在去查找。注意的是查找的值没有考虑正负,最后解的数量要乘以16.

#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
typedef long long ll;
const int MAXN = 3000008;
using namespace std;
int hash1[MAXN];
int a,b,c,d,x1,x2,x3,x4,ans=0;
int main () {
	while(~scanf ("%d%d%d%d", &a, &b, &c, &d)) {
		if(a>0&&b>0&&c>0&&d>0) {
			puts("0");

		} else if(a<0&&b<0&&c<0&&d<0) {
			puts("0");

		} else {
			memset(hash1,0,sizeof hash1);
			ans=0;
			for(x1=1; x1<=100; x1++) {
				for(x2=1; x2<=100; x2++) {
					hash1[a*x1*x1+b*x2*x2+2000000]++;
				}
			}
			for(x3=1; x3<=100; x3++) {
				for(x4=1; x4<=100; x4++) {
				ans+=hash1[2000000-c*x3*x3-d*x4*x4];
				}
			}
			printf("%d\n",ans*16);
		}
		

	}
	return 0;
}

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse
it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you
passed it on to your inexperienced team-mate. When the contest is almost over, you find out that
that problem still isn’t solved. The problem with the code is that the strings generated are often not
palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing
that the situation is desperate, you decide to simply write some additional code that takes the output
and adds just enough extra characters to it to make it a palindrome and hope for the best. Your
solution should take as its input a string and produce the smallest palindrome that can be formed by
adding zero or more characters at its end.

Input
Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of
upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than
or equal to 100,000.

Output
For each line of input, output will consist of exactly one line. It should contain the palindrome formed
by adding the fewest number of extra letters to the end of the corresponding input string.

Sample Input
aaaa
abba
amanaplanacanal
xyz

Sample Output
aaaa
abba
amanaplanacanalpanama
xyzyx

这题的思路是指要把要求的字符串的字符hash一下,在去查找不同的地方,标记一下,再补剩余的元素就行了。

#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
typedef long long ll;
const int MAXN = 30000008;
using namespace std;
unsigned long long int h[MAXN],po[MAXN],h_[MAXN],p=131;char s[MAXN];
unsigned long long get_ha(int l,int r) {
	return h[r]-h[l-1]*po[r-l+1];
}
unsigned long long get_ha2(int l,int r ) {
	return h_[r]-h_[l-1]*po[r-l+1];
}
void solve() {
	int len=strlen(s+1);
	po[0]=1;
	h[0]=0;
	for(int i=1;i<MAXN;i++)po[i]=po[i-1]*p;
	for(int i=1; i<=len; i++) {
		h[i]=h[i-1]*p+s[i];
		h_[i]=h_[i-1]*p+s[len-i+1];
	} 
	int minlen=0;
	for(int i=1; i<=len; i++) { //jiansuo
		unsigned long long pre=get_ha(i,len);
		unsigned long long suf=get_ha2(1,len-i+1);
		if(pre==suf) {
			minlen=i-1;
			break;
		}
	}
	printf("%s",s+1);
	for(int i=minlen; i>=1; i--) {
		printf("%c",s[i]);
	}	
	puts("");
	//cout<<minlen<<endl;
}
int main () {
	while(~scanf("%s",s+1)) {
		solve();
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值