[UVA][Trie] 10745 - Dominant Strings@Morris' Blog|PChome Online 人新台
2013-04-27 11:50:47| 人2,464| 回24 | 上一篇 | 下一篇

[UVA][Trie] 10745 - Dominant Strings

0 收藏 0 0 站台

Problem F
Dominant Strings
Input: standard input
Output: standard output
Time Limit: 2 seconds

Given two strings s1 and s2, we say that s1 dominates s2 if the (multi)set of characters in s1 is a proper superset of the (multi)set of characters in s2. For instance, "acmicpc" dominates "camp", but it does not dominate "chimp" or "macpac". For a set S of strings, we call the strings in S which are not dominated by any string in S the dominant strings of S (even if they do not dominate any strings in S).

Now, your task is simply to find all the dominant strings of a set of strings.

Input

The input contains a single set of strings, with one string per line. Each string consists of at least one and at most ten lower-case alphabetic characters. There will be at most 15000 strings, and no two strings will be identical. Input is terminated by end-of-file.

 

Output

Output consists of all dominant strings in the input set, in alphabetical order, one word per line.

 

Sample Input                           Output for Sample Input

acmicpc
cccp
macpac
chimp
camp
 
acmicpc
chimp
macpac

 


Problem setter: Per Austrin, KTH

Special Thanks: Mohammad Sajjad Hossain

目明:

就如同支配一,一字串A如何支配另一字串B?可以字串A挑符出排列後成字串B。
就可以字串A支配字串B。

找出不被支配的所有字串按照字母大小排序。

解法分析:

看一下目定的件, 每字串度小等於 10。
比起 O(n*n*len) 的暴力判,我需要做法。

hint: 度小等於 10 的最多能支配多少字串?Ans. 32 (因2^5)

最情 aabbccddee, 如此一度就降低很多了。

那如何快速查找的被支配字串?使用 Trie,
建立於每字串建立 (0,0, ..., 0), 26 性的度, 它插入 Trie 中。

例如 aabb, 支配 aab, 就在 Trie 中查找 (2,1,0, ..., 0) 的所有情其除。

× aabb, bbaa 互相支配, 也不是答案。

#include <stdio.h>
#include <string.h>
#include <set>
#include <iostream>
#include <queue>
using namespace std;
char s[15000][15];
int notclear[15000];
struct Node {
    Node *next[11];
    vector<int> label;
    Node() {
        label.clear();
        memset(next, 0, sizeof(next));
    }
};
void insertTrie(int str[], Node *root, int label) {
    static Node *p;
    static int i, idx;
    p = root;
    for(i = 0; i < 26; i++) {
        idx = str[i];
        if(p->next[idx] == NULL) {
            p->next[idx] = new Node();
        }
        p = p->next[idx];
    }
    p->label.push_back(label);
}
void clearTrie(int str[], Node *root) {
    static Node *p;
    static int i, idx;
    p = root;
    for(i = 0; i < 26; i++) {
        idx = str[i];
        if(p->next[idx] == NULL)
            return;
        p = p->next[idx];
    }
    for(vector<int>::iterator it = p->label.begin();
        it != p->label.end(); it++)
        notclear[*it] = 1;
    p->label.clear();
}
void freeTrie(Node *root) {
    queue<Node*> Q;
    Node *nd;
    while(!Q.empty()) {
        nd = Q.front(), Q.pop();
        for(int i = 0; i <= 10; i++) {
            if(nd->next[i] != NULL)
                Q.push(nd->next[i]);
        }
        nd->label.clear();
        delete nd;
    }
}
int cnt[26] = {}, cnt2[26];
Node *root = new Node();
set<string> R;
void dfs(int idx, int flag) {//backtrack Dominant
    if(idx == 26) {
        if(flag)
            clearTrie(cnt2, root);
        return ;
    }
    int i;
    for(i = 0; i <= cnt[idx]; i++) {
        cnt2[idx] = i;
        dfs(idx+1, flag|(i != cnt[idx]));
    }
}
void dfsfind(Node *p) {
    int i;
    for(i = 0; i < 11; i++)
        if(p->next[i] != NULL)
            dfsfind(p->next[i]);
    if(p->label.size() == 1)
        R.insert(s[p->label[0]]);
}
int main() {
    int n = 0, i, j;
    while(gets(s[n])) {
        //strlen(s) <= 10
        memset(cnt, 0, sizeof(cnt));
        for(i = 0; s[n][i]; i++)
            cnt[s[n][i]-'a']++;
        insertTrie(cnt, root, n);
        n++;
    }
    for(i = 0; i < n; i++) {
        if(!notclear[i]) {
            memset(cnt, 0, sizeof(cnt));
            for(j = 0; s[i][j]; j++)
                cnt[s[i][j]-'a']++;
            dfs(0, 0);
        }
    }
    dfsfind(root);
    freeTrie(root);
    for(set<string>::iterator it = R.begin();
        it != R.end(); it++)
        puts((*it).c_str());
    return 0;
}
/*
acmicpc
cccp
macpac
chimp
camp
*/

台: Morris
人(2,464) | 回(24)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][二分+maxflow二分匹配] 10804 - Gopher Strategy
此分上一篇:[UVA][最大矩形] 10043 - Chainsaw Massacre

seo
Pleasant write-up, it is an incredibly trendy blog site that you've got in this article, sustain the favorable do the job, will likely be returning. thca flower hemp http://www.veronapress.com/top-7-strongest-thca-flower-strains-on-the-market-2024/article_3beaac3a-75f0-11ef-a04a-1bb0d5193cfd.html
2025-05-29 22:14:13
seo
Many thanks with regard to supplying current improvements concerning the issue, We anticipate study much more. pomegranate thc gummies http://www.leadvilleherald.com/article_d2171f22-657e-11ef-8ed1-cbb4abbf8168.html
2025-06-16 19:46:10
asddfd
May possibly fairly recently up and running an important web log, the data one offer you on this web site contains given a hand to all of us substantially. Bless you designed for your current precious time & get the job done. IPTV UK http://xtremehd-uk.uk/
2025-07-08 16:09:22
asdasda
Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking. mawartoto http://drcandicemd.com/disclaimer/
2025-07-14 18:50:52
seo
Excellent publish, Thanks with regard to discussing This particular understanding. Wonderfully created post, if perhaps just about all writers provided exactly the same degree of content material while you, the web will be a far better location. Make sure you continue the good work! 【エロ漫画】童マチアプ体 http://erokamikagura.com/24418/
2025-07-21 20:17:52
seo
Excellent publish, Thanks with regard to discussing This particular understanding. Wonderfully created post, if perhaps just about all writers provided exactly the same degree of content material while you, the web will be a far better location. Make sure you continue the good work! cute halloween panties http://www.amazon.com/dp/B0F3NQYDZF
2025-07-22 17:03:27
seo
Excellent publish, Thanks with regard to discussing This particular understanding. Wonderfully created post, if perhaps just about all writers provided exactly the same degree of content material while you, the web will be a far better location. Make sure you continue the good work! magnumslot http://magnumini.com/
2025-07-23 18:53:30
seo
This can be the words My group is selecting in all places. Bless you for one's web log, Freezing enroll your site. It's a attractive web log. inch. https://www.reddit.com/r/smallbusiness/comments/1lk5955/any_recommendations_for_managing_a_growing/ http://www.reddit.com/r/smallbusiness/comments/1lk5955/any_recommendations_for_managing_a_growing/
2025-07-23 20:34:15
ASDADS
I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. Family Tree http://www.evaheld.com/
2025-07-23 20:51:31
seo
This can be the words My group is selecting in all places. Bless you for one's web log, Freezing enroll your site. It's a attractive web log. inch. bachelorette lingerie gifts https://www.amazon.com/dp/B0DY8BH5NM
2025-07-24 18:44:07
seo
Excellent publish, Thanks with regard to discussing This particular understanding. Wonderfully created post, if perhaps just about all writers provided exactly the same degree of content material while you, the web will be a far better location. Make sure you continue the good work! Future Of B2B Travel http://blogs.lordstravelsonline.com/future-of-b2b-travel/
2025-07-29 20:30:49
ASDASD
Merely a smiling visitant here to share the love (:, btw outstanding style. skinpres t http://skinprest.com/
2025-08-03 17:47:12
asdads
I’m glad to become a visitor in this pure site, regards for this rare info! Rankexpert http://rankexpert.net/
2025-08-05 17:25:52
asds
. . , . ! https://www.karaoke-songpa.com
2025-08-06 18:49:09
sdfdsf
hello!! Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community. agentotoplay http://agentotoplayid.com
2025-08-12 19:49:04
dfdsd
This is the type of information I’ve long been trying to find. Thank you for writing this information. duit66 http://golfclubexpert.com/
2025-08-12 19:49:24
sdsdfd
Thankyou for this wondrous post, I am glad I observed this website on yahoo. Gsc108 http://ruuvi.it.com/
2025-08-12 19:49:42
(悄悄)
2025-08-27 16:29:54
ASDASD
If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you. click this link http://caffeyolly.com/
2025-08-28 19:25:06
Thander
You have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!! toro99 http://toro99.pro/ Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. exo123.store https://exo123.store/
2025-09-01 19:30:58
gggf
I really impressed after read this because of some quality work and informative thoughts . I just wanna say thanks for the writer and wish you all the best for coming!. search engine optimization services http://rankexpert.net/services/performance-marketing/
2025-09-02 17:39:35
DSFDSFDS
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. satta king disawar http://a7-satta1.com
2025-09-02 17:46:31
asdfdasdfas
This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! http://cashpay.uriweb.kr/
2025-09-13 16:17:45
ASASD
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. http://www.sinchon517.com
2025-09-16 15:52:26
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86