P3370-[模板]字符串哈希【hash】
生活随笔
收集整理的這篇文章主要介紹了
P3370-[模板]字符串哈希【hash】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
評測記錄:https://www.luogu.org/recordnew/lists?uid=52918&pid=P3370
大意
輸出若干個字符串,求輸入的字符串的總個數。
解題思路
就是用hash表就好了。
code
#include<cstdio> #include<iostream> #include<string> #define p 30001 using namespace std; int n,ans; string s,hash[p]; int hashmath(string x)//哈希函數 {int ans=0;for (int i=0;i<x.size();i++){ans=(ans+x[i])%p;}return ans%p; } int locate(string x)//尋找插入位置 {int wz=hashmath(x);int i=0;while (i<p && hash[(wz+i)%p]!=x && hash[(wz+i)%p]!="")i++;return (wz+i)%p; } int main() {scanf("%d",&n);for(int i=1;i<=n;i++){cin>>s;int wz=locate(s);if(hash[wz]!=s){hash[wz]=s;ans++;}//有新字符串}printf("%d",ans); } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的P3370-[模板]字符串哈希【hash】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软正式推出 Win11 23H2,AI
- 下一篇: P3501-[POI2010]ANT-A