HDU-1116-Play on Words (并查集 +欧拉回路)

原题链接
http://acm.hdu.edu.cn/showproblem.php?pid=1116
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word acm'' can be followed by the wordmotorola’’. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence “Ordering is possible.”. Otherwise, output the sentence “The door cannot be opened.”.
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
题意:
有n个字符串,想让这n个字符串首尾相连形成一个长字符串,要求连接时,下一个字符串的首个字符一定与前一个字符串的最后一个字符串相同。问能否形成这样的长字符串。
题解:
类似于形成欧拉回路,要求每一种出现过的字符的入度等于出度,与此同时,也可以存在一个入度比出度大一和一个出度比入度大一的字符串作为长字符串的首尾,其余的插在中间。但仅仅这样是不行的,还需要能够连接起来,即只有棵树,如果有多棵树,也不能够连起来。(这里就用到了并查集)
附上AC代码:

#include <iostream>
#include <cstring>
#include <map>
#include <cmath>
using namespace std;
int f[26],vis[26];
int findf(int i)//寻找根节点
{
    return f[i]==i?i:findf(f[i]);
}
void setf(int i,int j)//设置关系
{
    int a=findf(i),b=findf(j);
    if(a!=b)
        f[a]=b;
}
int main()
{
    ios::sync_with_stdio(false);
    int t,n;
    char s[1005];
    cin>>t;
    while(t--)
    {
        int cnt=0;//记录有几棵树,如果超过一棵,则一定不能联通
        int cnst=0;//入读比出度大一的个数
        int cned=0;//出读比入度大一的个数
        int flag=0;//标记能否满足题意
        char tmp='a';
        map<char,int> st;//定义字符与数量的关系,用来记录入度和出度
        map<char,int> ed;
        memset(vis,0,sizeof(vis));//初始化
        for(int i=0;i<26;i++)//初始化
            f[i]=i;
        for(int i=1;i<=26;i++)//初始化
        {
            st[tmp]=0;
            ed[tmp]=0;
            tmp++;
        }
        cin>>n;
        for(int i=0;i<n;++i)
        {
            cin>>s;
            int len=strlen(s);
            int start=s[0]-'a';
            int ends=s[len-1]-'a';
            st[s[0]]++;
            ed[s[len-1]]++;
            vis[start]=1;//出现过
            vis[ends]=1;
            setf(start,ends);//连接
        }
        for(int i=0;i<26;i++)//查询有几颗子树
            if(vis[i] && f[i]==i)
                cnt++;
        if(cnt>1)//超过一棵树,则一定无法满足题意
        {
           cout<<"The door cannot be opened."<<endl;
           continue;
        }
        tmp='a';
        for(int i=1;i<=26;++i)//判断能否练成一条线
        {
            int d=abs(st[tmp]-ed[tmp]);//记录每一个字符的入度与出度的差的绝对值
            if(d==0)//刚好相同,不用管
            {
                tmp++;
                continue;
            }
            else
            {
                if(d==1)//差值为1
                {
                    if(st[tmp]-ed[tmp]==1)//入度比出度大
                        cnst++;
                    else//入度比出度小
                        cned++;
                }
                if(cnst>1||cned>1)//入度出度不等的点超过1个,一定不能满足题意
                {
                    flag=1;
                    break;
                }
                if(d>=2)//入度和出度差值大于2,一定不能连成线
                {
                    flag=1;
                    break;
                }
            }
            tmp++;
        }
        if(cnst+cned==1)//如果只有一个入度比出度大的则不行
            flag=1;
        if(flag)
            cout<<"The door cannot be opened."<<endl;
        else
            cout<<"Ordering is possible."<<endl;
    }
    return 0;
}

欢迎评论!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值