NYOJ 单词拼接

这里写图片描述
这里写图片描述
AC情况:
这里写图片描述
有点气
理解:代码搬的别的大佬的,有的地方还不太理解,得继续研究。。把单词的开头和结尾两个字母看作结点,单词是线,按这个规则构建图,先根据有多少个字母落单,判断能不能直接输出,不能的话就用深度优先搜索遍历,边排序边遍历,存到栈里,如果不能遍历全部,还是输出(比如aab,baa,ccc),否则输出栈。

#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
#include<cstring>
#include<cstdio>
using namespace std;

char word[1005][31];

typedef struct {
    char* pword;
    char f;
}Word;
Word no[1005];

int M,a,b;
int mapp[26][1005];
int stackk[1005];

struct LL {
    int num;
    LL *pre;
}temp,*Node;

int cmp(const void *a,const void *b) {
    return strcmp((*(Word *)a).pword, (*(Word *)b).pword);                  
}

void sort(int num) {
    int i = 1, large, left, right, j;
    do {
        j = 0;
        left = 2 * i;
        right = left + 1;
        large = i;
        if (left <= mapp[num][0] && mapp[num][left] < mapp[num][i]) large=left;
        if (right <= mapp[num][0] && mapp[num][right] < mapp[num][large]) large = right;
        if (i != large) {
            b = mapp[num][i];
            mapp[num][i] = mapp[num][large]; 
            mapp[num][large] = b;
            i = large;
            if (2 * i <= mapp[num][0])j = 1;
        }
    } while (j);
}

void dfs(int num) {
    LL *last; last = (LL*)malloc(sizeof(LL));
    int i = num;
    Node = NULL;
    while (mapp[i][0] != 0) {
        M--;
        last = (LL*)malloc(sizeof(LL));
        last->num = mapp[i][1];
        last->pre = Node;
        Node = last;
        a = no[mapp[i][1]].f - 'a';
        mapp[i][1] = mapp[i][mapp[i][0]--];
        sort(i); 
        i = a;
    }
    while (last != NULL) {
        a = no[last->num].pword[0] - 'a';
        stackk[++stackk[0]] = last->num;
        Node = last;
        last = last->pre;
        if (mapp[a][0] != 0) dfs(a);
    }
}

int main() {
    int N;
    scanf("%d", &N);
    while (N--) {
        scanf("%d", &M);
        for (int i = 0; i < M; i++) {
            scanf("%s", word[i]);
            no[i].pword = word[i];
        }
        for (int i = 0; i < 26; i++) mapp[i][0] = 0;
        qsort(no, M, sizeof(no[0]), cmp);
        for (int i = 0; i < 29; i++) stackk[i] = 0;
        b = 25;
        for (int i = 0; i < M; i++) {
            a = no[i].pword[0]-'a';
            mapp[a][++mapp[a][0]] = i;
            no[i].f = no[i].pword[strlen(no[i].pword) - 1];
            stackk[a]++;
            stackk[no[i].f-'a']--;
            if (a < b) b = a;
        }
        for (int i = 0; i < 26; i++) {
            if (stackk[i] == -1) { stackk[26]++; continue; };
            if (stackk[i] == 1) { stackk[27]++; continue; b = i; };
            if (stackk[i] == 0) stackk[28]++;
        }
        a = 0;
        if (stackk[28] == 26 || (stackk[26] == 1 && stackk[27] == 1 && stackk[28 == 24]))a = 1;
        if (!a) { printf("***\n"); continue; }
        stackk[0] = 0; 
        dfs(b);
        if(M){ printf("***\n"); continue; }
        b = stackk[0];
        printf("%s", no[stackk[b]].pword);
        for (int i = b - 1; i > 0; i--)
            printf(".%s", no[stackk[i]].pword);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值