problem Description
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists have to solve it to open that doors. Because there isNo other-to-open the doors, the puzzle isVery important forus. There isA large number of magnetic plates on every door. Every plate have one word written on it. The plates must is arranged into a sequenceinchSuch a-every word begins with the same letter asThe previous word ends. For example, the word ' ACM '"'Can be followed by the word ' Motorola"'. Your Task isTo write a computer program that would read the list of words and determine whether it ispossible to arrange all of the platesincha sequence (according to the given rule) and consequently to open the door.
Input
The 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 and 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 of the word contains at least of 1000 lowercase characters, that means onl Y letters a " through " Z would appear in the word. The same word may appear several times in the list.
Output
is inch is from "ordering is possible. " " The door cannot be opened. "
Sample Input
3 2 ACMIBM 3 Acmmalformmouse 2 Okok
Sample Output
is possible. The door cannot be opened.
Source
Central Europe 1999
First convert the tail to the number. Calculates the degree of entry for each point. Mark 0-26 which points have appeared.
Determine if it is Oraton road. 1. Root <=1
2, there are 0 in the degree of different | | 2 in degrees different and 1 difference
All the rest is not Oraton road.
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 1006 at #defineINF 1e12 - intN; - CharS[n]; - inta[ $]; - intb[ $]; - intvis[ $],fa[ $]; in voidinit () { - for(intI=0;i< -; i++){ tofa[i]=i; + } - } the intFindintx) { * returnfa[x]==x?x:fa[x]=find (Fa[x]); $ }Panax Notoginseng voidMergeintXinty) { - introot1=find (x); the intRoot2=find (y); + if(ROOT1==ROOT2)return; Afa[root1]=Root2; the } + intMain () - { $ intT; $scanf"%d",&t); - while(t--){ -Init ();//and I forgot ... theMemset (A,0,sizeof(a)); -memset (b,0,sizeof(b));Wuyimemset (Vis,0,sizeof(Vis)); thescanf"%d",&n); - for(intI=0; i<n;i++){ Wuscanf"%s", s); - intlen=strlen (s); About intnum1=s[0]-'a'; $ intnum2=s[len-1]-'a'; - merge (num1,num2); -b[num1]++; -a[num2]++; Avis[num1]=1; +vis[num2]=1; the } - intCnt=0; $ for(intI=0;i< -; i++){ the if(Find (i) ==i &&Vis[i]) { thecnt++; the } the } - if(cnt>1){ inprintf"The door cannot be opened.\n"); the Continue; the } About inttmp=0; the intp[ -]; the for(intI=0;i< -; i++){ the if(A[i]!=b[i] &&Vis[i]) { +p[tmp]=i; -tmp++; the }Bayi } the if(tmp==0){ theprintf"Ordering is possible.\n"); - Continue; - } the if(tmp==2&& (a[p[0]]-b[p[0]]==1&& b[p[1]]-a[p[1]]==1|| b[p[0]]-a[p[0]]==1&& a[p[1]]-b[p[1]]==1)){ theprintf"Ordering is possible.\n"); the Continue; the } -printf"The door cannot be opened.\n"); the } the return 0; the}
View Code
HDU 1116 Play on Words (Oraton Road)