IT民工
加油!

字典树存储,用并查集判连通, 欧拉回路判通路。

/*Accepted    59208K    422MS    C++    1191B    2012-08-02 11:38:21*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
    int next[26];
    int cnt, ord;
}Trie;

Trie t[3000000];
int tp, op;
int p[510000];
char s1[20], s2[20], odd;
int find(int x)
{
    return p[x] == x ? x : (p[x] = find(p[x]));
}

int insert(char *x, int site)
{
    if(*x)
    {
        if(!t[site].next[*x - 'a'])
            t[site].next[*x - 'a'] = ++ tp;
        return insert(x + 1, t[site].next[*x - 'a']);
    }
    else{
        if(t[site].cnt & 1)
            odd --;
        else
            odd ++;
        if(!t[site].cnt)
            t[site].ord = ++ op;
        t[site].cnt ++;
        return t[site].ord;
    }
}

int judge()
{
    int i;
    for(i = 2; i <= op; i ++)
        if(find(i) != find(i - 1))
            return 0;
    return odd <= 2;
}

int main()
{
    int a, b, i;
    tp = op = 0;
    for(i = 0; i < 510000; i ++)
        p[i] = i;
    while(scanf("%s%s", s1, s2) == 2)
    {
        a = insert(s1, 0);
        b = insert(s2, 0);
        p[find(a)] = find(b);
    }
    if(judge())
        printf("Possible\n");
    else
        printf("Impossible\n");
    return 0;
}
posted on 2012-08-02 11:43  找回失去的  阅读(176)  评论(0编辑  收藏  举报