栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2349 Mix and Build

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2349 Mix and Build

#include<iostream>#include<vector>#include<cstdio>#include<set>#include<map>#include<algorithm>#include<string.h>using namespace std;enum {    SIZ = 10008,    ALP = 26,};struct Node{    string w;    int c, p;};Node tree[SIZ];int pos[SIZ];struct Cmp {    bool operator()(const int& a, const int& b) const{        return tree[a].w.length() < tree[b].w.length();    }}cmp;char alp[ALP+1];map<string, int> tab;int top, save, num;void getKey(const string &s, string&k){    memset(alp, 0, sizeof(alp));    int i;    for(i=0; s[i]; i++){        alp[s[i]-'a']++;    }    for(i=0; i<ALP; i++){        alp[i] += 'a';    }    k = alp;}void traceback(int s){    vector<int> v;    while(s!= -1){        v.push_back(s);        s = tree[s].p;    }    for(int i=v.size()-1; i>=0; i--){        printf("%sn", tree[v[i]].w.c_str());    }}void fun(){    int i,j, t;    string key;    map<string, int>::iterator it;    top = save = -1;    for(i=0; i<num; i++){        t = pos[i];        getKey(tree[t].w, key);        it = tab.find(key);        if(it != tab.end()) continue;        for(j=0; key[j]; j++){ if(key[j] == 'a') continue; key[j]--; it = tab.find(key); if(it!=tab.end() && tree[t].c <= tree[it->second].c){     tree[t].c = tree[it->second].c + 1;     tree[t].p = it->second; } key[j]++;        }        tab.insert(make_pair(key, t));        if(top < tree[t].c){ top = tree[t].c; save = t;        }    }    traceback(save);}void readIn(){    static char buf[24];    tab.clear();    num=0;    while(gets(buf) && buf[0]!=0){        tree[num].w = buf;        tree[num].c = 0;        tree[num].p = -1;        pos[num] = num;        num++;    }    sort(pos, pos+num, cmp);}int main(){    int tst;    scanf("%d ", &tst);    while(tst--){        readIn();        fun();        if(tst) printf("n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377735.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号