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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj2418map或者字典树

發布時間:2025/6/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj2418map或者字典树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ?給你一些串,然后求出每個串出現的概率。


思路:

? ? ?簡單題目,做法也很多,我用字典樹做了下,然后又用map做了下,其實這個題目我感覺直接排序一遍之后線性輸出應該是最簡單最快的(這個沒敲),就是只是排序的時間復雜度而已O(n*log(n)*len)字典序的排序時間復雜度記得*len.


字典樹829MS #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm>using namespace std;typedef struct Tree {Tree *next[129];int v; }Tree;typedef struct {char s[32]; }SS;Tree root; SS S[10005];bool camp(SS a ,SS b) {return strcmp(a.s ,b.s) < 0; }void BuidTree(char *str) {int len = strlen(str);Tree *p = &root ,*q;for(int i = 0 ;i < len ;i ++){int id = str[i];if(p -> next[id] == NULL){q = (Tree *)malloc(sizeof(root));q -> v = 0;for(int j = 0 ;j <= 128 ;j ++)q -> next[j] = NULL;p -> next[id] = q;p = p -> next[id];}else p = p -> next[id];}p -> v ++; }int Query(char *str) {int len = strlen(str);Tree * q = &root;for(int i = 0 ;i < len ;i ++){int id = str[i];q = q -> next[id];if(q == NULL) return 0;}return q -> v; }int main () {int i ,n ,id;char str[35];n = id = 0;for(i = 0 ;i <= 128 ;i ++)root.next[i] = NULL;while(gets(str)){if(!Query(str)){id ++;int len = strlen(str);for(int j = 0 ;j <= len ;j ++)S[id].s[j] = str[j];}BuidTree(str);n ++;}sort(S + 1 ,S + id + 1 ,camp);for(i = 1 ;i <= id ;i ++)printf("%s %.4lf\n" ,S[i].s ,Query(S[i].s) * 100.0 / n);return 0; }map 1500ms #include<map> #include<string> #include<stdio.h> #include<string.h> #include<algorithm>using namespace std;typedef struct {char s[35]; }SS;SS S[10005]; map<string ,int>mark;bool camp(SS a ,SS b) {return strcmp(a.s ,b.s) < 0; }int main () {mark.clear();char str[35];int id = 0 ,n = 0;while(gets(str)){if(mark[str] ++ == 0){int len = strlen(str);id ++;for(int i = 0 ;i <= len ;i ++)S[id].s[i] = str[i];}n ++;}sort(S + 1 ,S + id + 1 ,camp);for(int i = 1 ;i <= id ;i ++){printf("%s %.4lf\n" ,S[i].s ,mark[S[i].s] * 100.0 / n);}return 0;}

總結

以上是生活随笔為你收集整理的poj2418map或者字典树的全部內容,希望文章能夠幫你解決所遇到的問題。

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