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

poj 3188 Cellphones

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

poj 3188 Cellphones

#include <stdio.h>#include <string.h>#define MAX_D 1024#define HASH_SIZE 65536struct node {    struct node *next;    int val, cnt;};int B, L, D;int map[256], ans[256], best;char dict[MAX_D][16];struct node nodes[MAX_D], *hash[HASH_SIZE];int nodes_cnt;int vis[HASH_SIZE], tm;inline void calc(){    int i, h, val, uni;    char *s;    struct node *t;    tm++;    nodes_cnt = 0;    for (i = 0; i < D; i++) {        val = 0;        for (s = dict[i]; *s; s++)  val = val*31 + map[*s] + 'a';        h = val & (HASH_SIZE - 1);        if (vis[h] != tm) { vis[h] = tm; hash[h] = NULL;        }        for (t = hash[h]; t; t = t->next) if (t->val == val)     break;        if (!t) { t = &nodes[nodes_cnt++]; t->val = val; t->cnt = 0; t->next = hash[h]; hash[h] = t;        }        t->cnt++;    }    uni = 0;    for (i = 0; i < nodes_cnt; i++)        if (nodes[i].cnt == 1) uni++;    if (uni > best) {        best = uni;        memcpy(ans, map, sizeof(map));    }}void dfs(int b, int l){    int i, cnt;    cnt = L - l - (B - b) + 1;    for (i = 0; i < cnt; i++)        map[l + 'A' + i] = b;    if (b == B - 1) {        calc();        return ;    }    for (i = cnt; i >= 1; i--)        dfs(b + 1, l + i);}int main(){    int i;    scanf("%d%d%d", &B, &L, &D);    for (i = 0; i < D; i++)        scanf("%s", dict[i]);    dfs(0, 0);    printf("%dn", best);    for (i = 'A'; i < 'A' + L; i++) {        if (ans[i] != ans[i - 1]) putchar('n');        putchar(i);    }    putchar('n');    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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