【leetcode】127. 单词接龙

 

struct Trie {
    int ch[27];
    int val;
} trie[50001];

int size, nodeNum;


int insert(char* s, int num) {
    int sSize = strlen(s), add = 0;
    for (int i = 0; i < sSize; ++i) {
        int x = s[i] - '`';
        if (trie[add].ch[x] == 0) {
            trie[add].ch[x] = ++size;
            //初始化下一个trie数据
            memset(trie[size].ch, 0, sizeof(trie[size].ch));
            trie[size].val = -1;
        }
        add = trie[add].ch[x];
    }
    trie[add].val = num;
    return num;
}

int find(char* s) {
    int sSize = strlen(s), add = 0;
    for (int i = 0; i < sSize; ++i) {
        int x = s[i] - '`';
        if (trie[add].ch[x] == 0) {
            return -1;
        }
        add = trie[add].ch[x];
    }
    return trie[add].val;
}

int addWord(char* word) {
    int ret = find(word);
    return (ret==-1)? insert(word, nodeNum++): ret;
}

int edge[30001][26];
int edgeSize[30001];

void addEdge(char* word) {
    int wordSize = strlen(word);
    int id1 = addWord(word);
    for (int i = 0; i < wordSize; ++i) {
        char tmp = word[i];
        word[i] = '`';
        int id2 = addWord(word);
        edge[id1][edgeSize[id1]++] = id2;
        edge[id2][edgeSize[id2]++] = id1;
        word[i] = tmp;
    }
}

int ladderLength(char* beginWord, char* endWord, char** wordList, int wordListSize) {
    size = nodeNum = 0;
    memset(trie[size].ch, 0, sizeof(trie[size].ch));
    trie[size].val = -1;
    memset(edgeSize, 0, sizeof(edgeSize));
    for (int i = 0; i < wordListSize; ++i) {
        addEdge(wordList[i]);
    }
    addEdge(beginWord);
    int beginId = find(beginWord);
    int endId = find(endWord);
    if (endId == -1) {
        return 0;
    }

    int* dis=(int*)malloc(nodeNum*sizeof(int));
    memset(dis, -1, sizeof(int)*nodeNum);
    dis[beginId] = 0;

    int* que=(int*)malloc(nodeNum*sizeof(int));

    int left = 0, right = 0;
    que[right++] = beginId;
    while (left < right) {
        int x = que[left++];
        for (int i = 0; i < edgeSize[x]; ++i) {
            if (dis[edge[x][i]] == -1) {
                dis[edge[x][i]] = dis[x] + 1;
                if (edge[x][i] == endId) {
                    return dis[edge[x][i]] / 2 + 1;
                }
                que[right++] = edge[x][i];
            }
        }
    }
    return 0;
}

 

posted @ 2020-12-20 10:08  温暖了寂寞  阅读(83)  评论(0编辑  收藏  举报