日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 861D - Polycarp's phone book 字典树/hash

發(fā)布時間:2025/4/9 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 861D - Polycarp's phone book 字典树/hash 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

輸入7e4個字符串,要求每個串提取一個子串來唯一表示

4s題可以hash暴力水過,大體思路就是把所有子串map自己的母串,過程中如果這個子串已有hash值就標-1

然后枚舉map元素,維護最小化一下就行了

唯一要注意的就是十進制哈希方式區(qū)分不了00和0

另一種做法是字典樹存下所有子串,

枚舉母串,枚舉子串,字典樹刪掉子串,樹中查子串個數(shù)是不是0,是的話維護一下最小就行了

復雜度勝在字典樹上短的先拿到?

主要還是因為字典樹沒個熟悉的板子所以想練練。。

#include<bits/stdc++.h> #include<stdio.h> #include<algorithm> #include<queue> #include<string.h> #include<iostream> #include<math.h> #include<set> #include<map> #include<vector> #include<iomanip> using namespace std; #define ll long long #define pb push_back #define FOR(a) for(int i=1;i<=a;i++) const int inf=0x3f3f3f3f; const int maxn=5e6+9; const int sigma=10; int mx; char a[70010][10],tmp[10],ans[10];struct NODE{int nxt[10];int val;void init(){val=0;memset(nxt,0,sizeof nxt);} }trie[maxn]; int node_cnt=0;void insert(char *s){int len=strlen(s);int now=0;for(int i=0;i<len;i++){int id=s[i]-'0';int to=trie[now].nxt[id];if(to==0){to=++node_cnt;trie[to].init();trie[now].nxt[id]=to;}trie[to].val+=1;now=to;}//trie[now].v=0; } void del(char *s){int len=strlen(s);int now=0;for(int i=0;i<len;i++){int id=s[i]-'0';int to=trie[now].nxt[id];//trie[to].init();trie[to].val--;now=to;} } int query(char *s){int len=strlen(s);int now=0;for(int i=0;i<len;i++){tmp[i]=s[i];int id=s[i]-'0';int to=trie[now].nxt[id];//if(to==-1)return -1;if(trie[to].val==0){if(i+1<mx){mx=i+1;tmp[i+1]='\0';strcpy(ans,tmp);break;}}now=to;}//return trie[now].val; }int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%s",a[i]);for(int j=0;j<9;j++)insert(a[i]+j);}for(int i=1;i<=n;i++){mx=10;for(int j=0;j<9;j++)del(a[i]+j);for(int j=0;j<9;j++)query(a[i]+j);for(int j=0;j<9;j++)insert(a[i]+j);printf("%s\n",ans);}}


轉載于:https://www.cnblogs.com/Drenight/p/8611254.html

總結

以上是生活随笔為你收集整理的Codeforces 861D - Polycarp's phone book 字典树/hash的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。