码迷,mamicode.com
首页 > 其他好文 > 详细

第八天知识点小结 训练小结

时间:2021-01-30 12:06:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:理解   abs   val   不能   ali   nal   现象   代码   ==   

今天是对于哈希的用法的一个小小的了解

哈希的用法主要适用于数据的查询之中,为了把原本时间复杂度比较高的变成o(1)去实现原本的算法

以下是正经解释:

HASH概述
?Hash其实是一种散列技术,散列技术是指在记录的存储位置和它的关键字之间建立一个确定的对应
关系f,使每一个关键字都对应一个存储位置。即:存储位置=f(关键字)。这样,在查找的过程中,
只需要通过这个对应关系f 找到给定值key的映射f(key)。只要集合中存在关键字和key相等的记录,
则必在存储位置f(key)处。我们把这种对应关系f 称为散列函数或哈希函数。
哈希冲突
?在理想的情况下,每一个 关键字,通过哈希函数计算出来的地址都是不一样的。但是在实际情况中,
我们常常会碰到两个关键字key1≠key2,但是f(key1) = f(key2), 这种现象称为冲突,并把key1和key2称
为这个散列函数的同义词。

 

题型分析:1.对于一些高次幂的数字,不能直接计算的时候,可以采用式子去化简一一对应起来

如:

A - Equations

 

Consider equations having the following form:

a*x1^2+b*x2^2+c*x3^2+d*x4^2=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.

InputThe 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.OutputFor 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

代码则如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn=1000000+5;
#define Please return
#define AC 0
int hush1[maxn];
int hush2[maxn];
int main()
{
int a,b,c,d;
long long ans=0;
while(scanf("%d %d %d %d",&a,&b,&c,&d)!=EOF)
{
memset(hush1,0,sizeof hush1);
memset(hush2,0,sizeof hush2);
ans=0;
if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0)
{
cout<<"0"<<"\n";
continue;
}
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
if(a*i*i+b*j*j>=0)
{
hush1[a*i*i+b*j*j]++;
}
else
{
hush2[abs(a*i*i+b*j*j)]++;
}
}
}//存储
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
if(-c*i*i-d*j*j>=0)
{
ans+=hush1[-c*i*i-d*j*j];
}
else
{
ans+=hush2[abs(-c*i*i-d*j*j)];
}
}
}
printf("%lld\n",ans*16);
}
Please AC;
}

 

题型2:字符串Hash

 

解题知识:

?Hash[r]=s[1]*pr+s[2]*pr-1…s[r]*p0
?Hash[l-1]=s[1]*pl-1+s[2]*pl-2…s[l-1]*p0
?Hash[l~r]=s[l]*pr-l+s[l+1]*pr-l-1…s[r]*p0
void get_hash(){
return ((hash[r]-hash[l-1]*pow(p,r-l+1))%mod+mod)%mod;
}

 

B - Extend to Palindrome

 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

 

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn=1000000+5;
#define Please return
#define AC 0
char zf[100000+5][maxn];
unsigned long long h[maxn],pt[maxn],h2[maxn];//ull取不到负数,而且能够自动取模
unsigned long long getha(int l,int r)
{
return h[r]-h[l-1]*pt[r-l+1];
}
unsigned long long getha2(int l,int r)
{
return h2[r]-h2[l-1]*pt[r-l+1];
}//
unsigned long long p=131;// 取任意一个质数 通常用131 13331 2333 //
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%s",zf[i]+1);
}
while(scanf("%s",zf+1)!=EOF)
{
int len=strlen(zf+1);
pt[0]=1;
for(int i=1;i<maxn;i++)
{
pt[i]=pt[i-1]*p;
}
for(int i=1;i<=len;i++)
{
h[i]=h[i-1]*p+zf[i];//正向hash数组
h2[i]=h2[i-1]*p+zf[len-i+1];//反向hash数组
}
int mlen=0;
for(int i=1;i<=len;i++)
{
unsigned long long pre=getha(i,len);
unsigned long long suf=getha2(1,len-i+1);
if(pre==suf)
{
mlen=i-1;
break;
}
} //枚举i~len==1~lenlen-i+1;
printf("%s",zf+1);
for(int i=mlen;i>=1;i--)
{
printf("%c",zf[i]);
}
printf("\n");
}
Please AC;
}
//Hash[r]=s[1]*pr+s[2]*pr-1…s[r]*p0 //可以理解为从1~r
//Hash[l-1]=s[1]*pl-1+s[2]*pl-2…s[l-1]*p0
//Hash[l~r]=s[l]*pr-l+s[l+1]*pr-l-1…s[r]*p0 //字符串的哈希公式
//void get_hash()
//{
//return ((hash[r]-hash[l-1]*pow(p,r-l+1))%mod+mod)%mod;
//} 上面的式子1减去 式子2*p的r-l+1次数 目的是为了消去前面的s【1】~s【l-1】

 

第八天知识点小结 训练小结

标签:理解   abs   val   不能   ali   nal   现象   代码   ==   

原文地址:https://www.cnblogs.com/xyisreallycuteeee/p/14346930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!