UVA ~ 10129 ~ Play on Words (欧拉路)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZscDst/article/details/80320576

题意:输入n(n≤100000)个单词,是否可以把所有这些单词排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母和上一个单词的第一个字母相同(例如acm,malform,mouse)。每个单词最多包含1000个小写字母。输入中可以有重复的单词。

【分析】

把字母看作结点,单词看成有向边,则问题有解,当且仅当图中有欧拉路径。前面讲过,有向图存在欧拉道路的条件有两个:底图(忽略边方向后得到的无向图)连通,且度数满足上面讨论过的条件(要求其每个点的入度等于出度,或者其中有一个点的出度比入度大1,另一个点的入度比出度大一这样就存在一条欧拉道路)。判断连通的方法有两种,一是之间介绍过的DFS,二是第11章中将要介绍的并查集。读者可以在学习完并查集之后根据自己的喜好选用。

以上内容来自《算法竞赛入门经典》


并查集:

#include<bits/stdc++.h>
using namespace std;
char str[1005];
int n, deg[30], f[30];//deg表示度数
bool vis[30];//某个字母是否出现过
int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); }
int main()
{
    int T; scanf("%d", &T);
    while (T--)
    {
        memset(deg, 0, sizeof(deg));
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < 26; i++) f[i] = i;//并查集初始化
        int cc = 26;//联通块数
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", str);
            int u = str[0] - 'a', v = str[strlen(str)-1] - 'a';
            deg[u]++; deg[v]--;
            vis[u] = vis[v] = 1;
            int root1 = Find(u), root2 = Find(v);
            if (root1 != root2) f[root1] = root2, cc--;
        }
        vector<int> d;
        for (int i = 0; i < 26; i++)
        {
            if (!vis[i]) cc--;//没出现过的字母
            else if (deg[i] != 0) d.push_back(deg[i]);
        }
        if (cc == 1 && (d.size() == 0 || (d.size() == 2 && (d[0] == 1 && d[1] == -1 || d[0] == -1 && d[1] == 1))))
            printf("Ordering is possible.\n");
        else printf("The door cannot be opened.\n");
    }
    return 0;
}
/*
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/


DFS:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
char str[MAXN][1005];
int n, deg[30], G[30][30], s, e;
void dfs(int u)
{
    for (int v = 0; v < 26; v++)
    {
        if (G[u][v])
        {
            G[u][v]--;
            dfs(v);
        }
    }
}
int main()
{
    int T; scanf("%d", &T);
    while (T--)
    {
        memset(deg, 0, sizeof(deg));
        memset(G, 0, sizeof(G));
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", str[i]);
            int u = str[i][0] - 'a', v = str[i][strlen(str[i])-1] - 'a';
            deg[u]++; deg[v]--;
            G[u][v]++;
            if (deg[u] == 1) s = u;
            if (deg[v] == -1) e = v;
        }
        vector<int> d;
        for (int i = 0; i < 26; i++)
            if (deg[i] != 0) d.push_back(deg[i]);
        dfs(s);
        bool flag = true;
        for (int i = 0; i < 26; i++)
        {
            for (int j = 0; j < 26; j++)
            {
                if (G[i][j])
                {
                    flag = false; break;
                }
            }
            if (!flag) break;
        }
        if (flag && (d.size() == 0 || (d.size() == 2 && (d[0] == 1 && d[1] == -1 || d[0] == -1 && d[1] == 1))))
            printf("Ordering is possible.\n");
        else printf("The door cannot be opened.\n");
    }
    return 0;
}
/*
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/



Play on Words

03-11

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.nThere 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 word "motorola". 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.nnnInputnnThe input consists of T test cases. The number of them (T) is given on the first line of the input. 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.nnnOutputnnYour 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. nnIf 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.".nnnSample Inputnn3n2nacmnibmn3nacmnmalformnmousen2noknoknnnSample OutputnnThe door cannot be opened.nOrdering is possible.nThe door cannot be opened.

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试