zoukankan      html  css  js  c++  java
  • hdu 1116 Play on Words

    Play on Words

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3149    Accepted Submission(s): 1002


    Problem Description
    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 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.
     
    
    
    Source
     
    
    
    Recommend
    Eddy
    //每个单词内部有个有相边,可连接的单词间有段有向边、
    并查集判断是否所有单词连通,且只能有一个连通集合

    #include <iostream> #include <map> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int in[27],out[27]; int F[27]; bool h[27]; int Find(int x) { if(x!=F[x]) return F[x]=Find(F[x]); return x; } char s[1002]; int main() { int i,n; int T; int a,b; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=26;i++) F[i]=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(h,0,sizeof(h)); for(i=0;i<n;i++) { scanf("%s",s); a=s[0]-'a'+1; b=s[strlen(s)-1]-'a'+1; h[a]=h[b]=1; in[a]++; out[b]++; a=Find(a); b=Find(b); if(a!=b) F[b]=a; } int cnt=0; for(i=1;i<=26;i++) if(h[i]&&F[i]==i) cnt++; if(cnt>1){ printf("The door cannot be opened.\n");continue;}//开始这里continu少了、郁闷 int f1=0,f2=0,f3=0; for(i=1;i<=26;i++) if(out[i]-in[i]==1) f1++; else if(in[i]-out[i]==1) f2++; else if(in[i]==out[i]) f3++; if(f1==1&&f2==f1&&f1+f2+f3==26) printf("Ordering is possible.\n"); else if(f1==0&&f1==f2&&f3==26) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } return 0; }
  • 相关阅读:
    洛谷 P2008 大朋友的数字
    [USACO10FEB]慢下来Slowing down
    HAOI2007 理想的正方形 单调队列
    滑动窗口
    双栈排序
    概率无向图模型与条件随机场的异同
    P-R曲线出现凹陷的原因
    MaskLab-实例分割(使用语义分割和方向特征精细化目标检测)
    模拟递归生成器
    递归生成器
  • 原文地址:https://www.cnblogs.com/372465774y/p/2732810.html
Copyright © 2011-2022 走看看