当前位置:网站首页>2.9 learning summary
2.9 learning summary
2022-06-26 04:35:00 【After all, I still walk alone】
Today, let's solve the Haas algorithm problem . It is also the template problem of this algorithm Just a little bit about my understanding .
The title is as follows .
A classic template question . The core of the algorithm is the basic application of hash algorithm . Through the hash algorithm . To make a unique hash value for each string . To distinguish strings . This is a template . That is, the difference in order is solved by multiplying each time by a number . Because for this continuous mathematical formula . The order affects the size of the results . For the number of digits, it means that each operation is performed or a value set randomly by oneself is added . Then you can implement this algorithm . Of course, this is not entirely accurate .
ans=(ans*base+(f)s[i])%m+p;That's the formula above .
To make a long story short , The hash algorithm used for the string , It is also a very simple reason for this algorithm . But make their hash values as small as possible , Not equal . The complete code is as follows ,
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long f;
f base=123;
f a[100000];
char s[100000];
int n,ans=1;
f m=12345678987654;
f p=555;
f hashe(char s[])
{
int len=strlen(s);
f ans=0;
for (int i=0;i<len;i++)
ans=(ans*base+(f)s[i])%m+p;
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",s);
a[i]=hashe(s);
}
sort(a,a+n);
for(int i=0;i<n-1;i++)
{
if(a[i]!=a[i+1])
ans++;
}
printf("%d",ans);
return 0;
} Then let's talk about another problem I wrote .
The title is as follows .

I used a function that I hadn't touched before , Namely map function . For external functions , I think that is to establish a mapping relationship . Or say map The function creates a free array . For arrays , The mapping relationship is implemented by the following symbols . in other words a[i]=? But for map Come on . This i Will no longer be numbers It is Custom data types ,
So for the above topic , We just need to use one map function , It can be solved by using the idea of bucket row . Specifically, mark all given names first . Then, when the second set of data is given, it is traversed . If it has been marked, it will be output ,ok. Output without tag wrong, Then for each output ok After the operation of , Then mark it . Cover it with . After traversing the data for the second time . It outputs repeat. The specific code is as follows ,
#include<map>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
map<string,int>a;
char s[10000000];
int n,m;
int main(){
scanf("%d",&n);
while(n--){
cin>>s;
a[s]=1;
}
scanf("%d",&m);
while(m--){
cin>>s;
if(a[s]==1){
printf("OK\n");
a[s]=2;}
else if(a[s]==2){
printf("REPEAT\n"); }
else printf("WRONG\n");
}
return 0;
}*** .
边栏推荐
- Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
- List of provinces, cities and counties in China
- Composer version rollback version switching
- Physical design of database design (2)
- PIP batch complete uninstall package
- 35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
- 35岁程序员炒Luna 千万资产3天归零,网友:和赌博一样
- Create alicloud test instances
- Yapi cross domain request plug-in installation
- 2022 talent strategic transformation under the development trend of digital economy
猜你喜欢

mysql高级学习(跟着尚硅谷老师周阳学习)
![Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]](/img/64/c3bb27a3711a6f0cc7b281d1a961af.jpg)
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]
![ctf [RoarCTF 2019]easy_ calc](/img/c7/16adb8e4b64a4be2129a6c53c5aaa7.jpg)
ctf [RoarCTF 2019]easy_ calc

SQL related knowledge - DDL

Construction of art NFT trading platform | NFT mall

Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
![There is no response to redirection and jump in the laravel constructor [original]](/img/6b/5d9d7fe1348892d01a87d04f122dfc.jpg)
There is no response to redirection and jump in the laravel constructor [original]

Database design (3): database maintenance and optimization

Nailing open platform - applet development practice (nailing applet client)

2021-01-31
随机推荐
MySQL enable logbin in Qunhui docker
Understand CGI and fastcgi
2021-01-31
Thinkphp6 implements a simple lottery system
There is no response to redirection and jump in the laravel constructor [original]
MySQL index details
微软禁止俄用户下载安装Win10/11
Tp6 controller does not exist: app\index\controller\index
Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?
防撤回测试记录
Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
Tp6 is easy to tread [original]
Development prospect and investment strategic planning report of global and Chinese PVC hose industry from 2022 to 2028
Sixtool- source code of multi-functional and all in one generation hanging assistant
Report on demand situation and development trend of China's OTC industry from 2022 to 2028
Knowledge of functions
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
小程序中实现视频通话及互动直播功能
SQL related knowledge - DDL