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; }
  • 相关阅读:
    dom4j解析XML时忽略DTD文件,加速文件解析过程
    mysql 中 in 语句参数个数
    N皇后 java
    springboot+jpa多表查询
    使用lua脚本在nginx上进行灰度流量转发
    RestTemplate将字符串以文件的方式上传
    在idea中编写自动拉取、编译、启动springboot项目的shell脚本
    逻辑回归调优方向
    流程图采用mindmanager进行绘制相关流程图体验较好
    尝试使用utool进行一些任务管理,例如ocr功能,使用讯飞ocr可以提高效率,替换图床
  • 原文地址:https://www.cnblogs.com/372465774y/p/2732810.html
Copyright © 2011-2022 走看看