当前位置:网站首页>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;
}
*** .
边栏推荐
- List of provinces, cities and counties in China
- Zhubo Huangyu: you can try these non-agricultural operation skills
- [geek challenge 2019] rce me
- How to use the configured slave data source for the scheduled task configuration class scheduleconfig
- Daily tests
- 基础查询
- Understand CGI and fastcgi
- Laravel access error could not be opened
- Report on demand situation and development trend of China's OTC industry from 2022 to 2028
- Thinkphp6 using kindeditor
猜你喜欢
Knowledge of SQL - database design, backup and restore
MySQL index details
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
Dameng database backup and restore
Gateway can not connect to tcp://127.0.0.1: Connection refused
六、项目实战---识别猫和狗
08_SpingBoot 集成Redis
Install cenos in the virtual machine
Group by and order by are used together
Your requirements could not be resolved
随机推荐
An unexpected attempt (Imperial CMS list template filters spaces and newlines in smalltext introduction)
Construction of art NFT trading platform | NFT mall
C generic
Ubuntu installs PostgreSQL and uses omnidb to view
[Qunhui] Internet access + custom port
Laravel uses phpword to generate word documents
Knowledge of functions
Thinkphp6 using kindeditor
Nabicat连接:本地Mysql&&云服务Mysql以及报错
Text horizontal alignment attribute text align and element vertical alignment attribute vertical align
Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
小程序中实现视频通话及互动直播功能
Realize video call and interactive live broadcast in the applet
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions
六、项目实战---识别猫和狗
Notes on enterprise wechat development [original]
Simple use of redis in laravel
Redis cache message queue
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]
Tp6 is easy to tread [original]