POJ2945 Find the clones

來源:互聯網
上載者:User

標籤:The   poj   pac   mes   get   一個   for   break   mem   

傳送門

Trie樹的模板題。我們把每個人都存在Trie樹裡面,然後再每個人跑一遍即可。注意一個人走過之後要清零,否則的話會被重複計算。

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<set>#include<queue>#define rep(i,a,n) for(int i = a;i <= n;i++)#define per(i,n,a) for(int i = n;i >= a;i--)#define enter putchar(‘\n‘)using namespace std;typedef long long ll;const int M = 40005;const int N = 500005;const ll mod = 1000000007;int read(){    int ans = 0,op = 1;    char ch = getchar();    while(ch < ‘0‘ || ch > ‘9‘)    {    if(ch == ‘-‘) op = -1;    ch = getchar();    }    while(ch >= ‘0‘ && ch <= ‘9‘)    {    ans *= 10;    ans += ch - ‘0‘;    ch = getchar();    }    return ans * op;}int n,m,ans[M];char s[M][50];struct trie{    int c[N][26],cnt,val[N];    bool vis[N];    void clear()    {        memset(c,0,sizeof(c));        memset(val,0,sizeof(val));        memset(vis,0,sizeof(vis));        cnt = 0;    }    void insert(char *p)    {        int l = strlen(p),u = 0;        rep(i,0,l-1)        {        int v = p[i] - ‘A‘;        if(!c[u][v]) c[u][v] = ++cnt;        u = c[u][v];        if(i == l-1) val[u]++;        }    }    void find(char *p)    {        int l = strlen(p),u = 0;        rep(i,0,l-1)        {        int v = p[i] - ‘A‘;        u = c[u][v];        if(i == l-1 && val[u]) ans[val[u]]++,val[u] = 0;        }    }}T;int main(){    while(1)    {    n = read(),m = read();    if(!n && !m) break;    T.clear();    memset(ans,0,sizeof(ans));    rep(i,1,n) scanf("%s",s[i]),T.insert(s[i]);    rep(i,1,n) T.find(s[i]);    rep(i,1,n) printf("%d\n",ans[i]);    }    return 0;}

 

POJ2945 Find the clones

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.