新聞| | PChome| 登入
2013-04-27 11:50:47| 人氣2,618| 回應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,618) | 回應(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
詳全文