uva10129(play with words)

来源:互联网 发布:网络营销策划书的步骤 编辑:程序博客网 时间:2024/04/29 16:24

题目描述:
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 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.
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 N that indicates the number of
plates (1 ≤ N ≤ 100000). Then exactly N lines 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个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同

题解:这一题是判断是否有欧拉回路,建图是关键,刚开始我是以单词为结点,如果两个单词能够前后相接则连边,这种方法不仅内存开销大,而且铁定超时,后来看了刘汝佳的题解,恍然大悟,用首尾字母为结点,单词为边,这是因为判断欧拉回路时的第一步判断该有向图的无向图是否连通时并不需要边的方向。至于出度和入度,在读入的时候就可以处理了,所以其实不需要建立一张完整的有向图,判断连通时可以借助并查集。

开始时建图错误的T了的代码:

#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>using namespace std;const int maxn=1e5+5;int vis[maxn],in[maxn],out[maxn];vector<int> G[maxn];void dfs(int u);int m;int main(){    //freopen("in.txt","r",stdin);    int n;    scanf("%d",&n);    while(n--)    {        vector<int> start[30];        vector<int> end[30];        memset(out,0,sizeof(out));        memset(in,0,sizeof(in));        scanf("%d",&m);        char s[1005];        for(int i=1;i<=m;i++)        {            scanf("%s",s);            int len=strlen(s);            start[s[0]-'a'+1].push_back(i);            end[s[len-1]-'a'+1].push_back(i);        }        for(int i=0;i<maxn;i++)        G[i].clear();        for(int i=1;i<=26;i++)        {            for(int j=0;j<start[i].size();j++)            {                for(int k=0;k<end[i].size();k++)                {                    if(start[i][j]!=end[i][k])                    {                        G[end[i][k]].push_back(start[i][j]);                        in[end[i][k]]++;                        out[start[i][j]]++;                    }                }            }        }        memset(vis,0,sizeof(vis));        int num=0;        for(int i=1;i<=m;i++)        if(!vis[i])        {            num++;            dfs(i);        }        if(num!=1)        {            printf("The door cannot be opened.\n");            continue;        }        bool flag1=true,flag2=true;        int innum=0,outnum=0;        for(int i=1;i<=m;i++)        if(in[i]!=out[i])        {            flag1=false;            break;        }        for(int i=1;i<=m;i++)        {            if(in[i]!=out[i])            {                if(in[i]==out[i]+1)                innum++;                else if(in[i]==out[i]-1)                outnum++;                else                {                    flag2=false;                    break;                }                if(innum>1||outnum>1)                {                    flag2=false;                    break;                }            }        }        if(flag1||flag2)        printf("The door cannot be opened.\n");        else printf("Ordering is possible.\n");    }}void dfs(int u){    vis[u]=1;    for(int i=0;i<G[u].size();i++)    if(!vis[G[u][i]])    dfs(G[u][i]);   }

只用了并查集判断是否连通的以上改版:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=1000+5;int pa[30];int deg[30],have[30];    //入度 ,单词是否出现 int vis[30];void init(void){    for(int i=1;i<=26;i++)    pa[i]=i;}int find(int x){    return pa[x]==x? x:pa[x]=find(pa[x]);}int main(){    //freopen("in.txt","r",stdin);    int cas;    scanf("%d",&cas);    while(cas--)    {         int n;         scanf("%d",&n);         init();         memset(deg,0,sizeof(deg));         memset(have,0,sizeof(have));         memset(vis,0,sizeof(vis));         char s[maxn];         for(int i=1;i<=n;i++)         {            scanf("%s",s);            int s1=s[0]-'a'+1;            int s2=s[strlen(s)-1]-'a'+1;            have[s1]=1,have[s2]=1;            deg[s1]++;            deg[s2]--;            s1=find(s1);            s2=find(s2);            if(s1!=s2)            pa[s1]=s2;         }         int k;         bool flag=true;         for(k=1;k<=26;k++)         {            if(have[k])            break;         }         for(int i=k;i<=26;i++)         {            if(have[i])            if(find(i)!=find(k))            {                flag=false;                break;            }         }         if(!flag)                                         {            printf("The door cannot be opened.\n");            continue;         }         bool flag1=true,flag2=true;         for(int i=1;i<=26;i++)                      //判断是否有欧拉回路          {            if(have[i])            {                if(deg[i]!=0)                flag1=false;            }         }         int in=0,out=0;         for(int i=1;i<=26;i++)                    //判断是否有欧拉道路          {            if(have[i])            {                if(deg[i]!=0&&deg[i]!=1&&deg[i]!=-1)                {                    flag2=false;                    break;                }                if(deg[i]==-1)                in++;                else if(deg[i]==1)                out++;                if(in>1||out>1)                {                    flag2=false;                    break;                }            }         }         if(in!=1||out!=1)         flag2=false;         if(flag1||flag2)         printf("Ordering is possible.\n");         else printf("The door cannot be opened.\n");    }}

后来ac的代码(利用了并查集的dfs,其实不需要dfs):

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=1000+5;int pa[30];int deg[30],have[30];    //入度 ,单词是否出现 int vis[30];void dfs(int u);void init(void){    for(int i=1;i<=26;i++)    pa[i]=i;}int find(int x){    return pa[x]==x? x:pa[x]=find(pa[x]);}int main(){    //freopen("in.txt","r",stdin);    int cas;    scanf("%d",&cas);    while(cas--)    {         int n;         scanf("%d",&n);         init();         memset(deg,0,sizeof(deg));         memset(have,0,sizeof(have));         memset(vis,0,sizeof(vis));         char s[maxn];         for(int i=1;i<=n;i++)         {            scanf("%s",s);            int s1=s[0]-'a'+1;            int s2=s[strlen(s)-1]-'a'+1;            have[s1]=1,have[s2]=1;            deg[s1]++;            deg[s2]--;            s1=find(s1);            s2=find(s2);            if(s1!=s2)            pa[s1]=s2;         }         int num=0;         for(int i=1;i<=26;i++)         {            if(have[i])            if(!vis[i])            {              num++;              dfs(i);            }         }         if(num!=1)                                //无向图连通块数目num          {            printf("The door cannot be opened.\n");            continue;         }         bool flag1=true,flag2=true;         for(int i=1;i<=26;i++)                      //判断是否有欧拉回路          {            if(have[i])            {                if(deg[i]!=0)                flag1=false;            }         }         int in=0,out=0;         for(int i=1;i<=26;i++)                    //判断是否有欧拉道路          {            if(have[i])            {                if(deg[i]!=0&&deg[i]!=1&&deg[i]!=-1)                {                    flag2=false;                    break;                }                if(deg[i]==-1)                in++;                else if(deg[i]==1)                out++;                if(in>1||out>1)                {                    flag2=false;                    break;                }            }         }         if(in!=1||out!=1)         flag2=false;         if(flag1||flag2)         printf("Ordering is possible.\n");         else printf("The door cannot be opened.\n");    }}void dfs(int u){    vis[u]=1;    for(int i=1;i<=26;i++)    {        if(i!=u&&have[i]&&(find(u)==find(i))&&!vis[i])        dfs(i);    }}

刘汝佳老师的代码:

// UVa10129 Play on Words// Rujia Liu// 题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同// 算法:把字母看作结点,单词看成有向边,则有解当且仅当图中有欧拉路径。注意要先判连通#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 1000 + 5;// 并查集(代码摘自《算法竞赛入门经典——训练指南》第三章)int pa[256];int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; } int used[256], deg[256]; // 是否出现过;度数int main() {  int T;  scanf("%d", &T);  while(T--) {    int n;    char word[maxn];    scanf("%d", &n);    memset(used, 0, sizeof(used));    memset(deg, 0, sizeof(deg));    for(int ch = 'a'; ch <= 'z'; ch++) pa[ch] = ch; // 初始化并查集    int cc = 26; // 连通块个数    for(int i = 0; i < n; i++) {      scanf("%s", word);      char c1 = word[0], c2 = word[strlen(word)-1];      deg[c1]++;      deg[c2]--;      used[c1] = used[c2] = 1;      int s1 = findset(c1), s2 = findset(c2);      if(s1 != s2) { pa[s1] = s2; cc--; }    }    vector<int> d;    for(int ch = 'a'; ch <= 'z'; ch++) {      if(!used[ch]) cc--; // 没出现过的字母      else if(deg[ch] != 0) d.push_back(deg[ch]);    }    bool ok = false;    if(cc == 1 && (d.empty() || (d.size() == 2 && (d[0] == 1 || d[0] == -1)))) ok = true;    if(ok) printf("Ordering is possible.\n");    else printf("The door cannot be opened.\n");  }  return 0;}
0 0
原创粉丝点击