CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February Long Contest
    • January CookOff
    • January Long Contest
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Play on Words

Play on Words

Problem code: WORDS1

  • Submit
  • All Submissions

All submissions for this problem are available.

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, equal to about 500) 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 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.".

Example

Sample input:

3
2
directi
codechef
3
skenzo
logicboxes
orderbox
2
ok
ok


Sample output:

The door cannot be opened.
Ordering is possible.
The door cannot be opened.

Warning: large Input/Output data, be careful with certain languages


Date: 2008-12-01
Time limit: 7

s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

jaganr @ 20 Jun 2009 01:11 AM

Can i get the input for which the program failed?

caa0502 @ 29 Jun 2009 11:51 PM

I would like to know to which test case the program failed. Please facilitate that.,

i0exception @ 30 Jun 2009 12:04 AM

Please read the FAQ at http://blog.codechef.com/2009/06/29/frequently-asked-questions/

Hi, I keep getting a "Time

uc_regents @ 27 Aug 2009 12:07 PM
Hi, I keep getting a "Time exceeded" error each time I submit my solution. I tried executing my solution on my home computer with a large input (75000 strings) and it computes the solution within 1 sec. The time limit posted for the solution is 7 secs. Why am I getting this error ? What size of input are you using ? ---- my output --- $ time ./a.out < NORM.DIC Ordering is possible. real 0m1.063s user 0m1.008s sys 0m0.036s ------ end output ----- Siva

You might want to try with a

admin @ 27 Aug 2009 01:15 PM

You might want to try with a large set of strings for which the door can be opened. If you keep getting TLE, then you either need to improve your algorithm or optimize it.

can i get the test cases for

ankit.it.mnit @ 3 Sep 2009 04:49 PM

can i get the test cases for this program. my program is running absolutely fine on my pc

Currently it is not possible

admin @ 3 Sep 2009 05:17 PM

Currently it is not possible to give test cases as then it would be possible to hardcode the values and submit the code.

Can we know how much time our

andy_chandani @ 23 Sep 2009 07:28 AM

Can we know how much time our problem took, when it exceeds the time limit?

That way we would know how much improvements we must make in our programs..

Currently this is not

admin @ 23 Sep 2009 02:16 PM

Currently this is not possible because as soon as the program exceeds the time limit, it is terminated. Some programs take an infinite amount of time to execute, so such an approach wouldn't really help.

i keep getting wrong asnwer

rishbeck8 @ 23 Sep 2009 08:28 PM

i keep getting wrong asnwer wrong answer :(

help needed....i m fed up.....

what should be the output

rishbeck8 @ 23 Sep 2009 09:03 PM

what should be the output when only one word is entered???

 

This should be evident from

admin @ 23 Sep 2009 09:06 PM

This should be evident from the problem statement :)

i tried both the things still

rishbeck8 @ 23 Sep 2009 09:09 PM

i tried both the things still not working ...!!!!

can u provide some more sample cases..I can't detect the flaw!!!!!

It should be easy to create

admin @ 23 Sep 2009 09:11 PM

It should be easy to create large random samples and test by first creating a very large sample which is correct in a greedy fashion and then using a random permutation of it as the test input.

i need some test cases where

rishbeck8 @ 27 Sep 2009 11:13 PM

i need some test cases where the program is failing.

i have even corrected the earlier error..changed everything

still couldn't figure out what is happening...

There's a very useful test

Syntaxide @ 28 Sep 2009 12:01 AM

There's a very useful test case for this problem on the forum.

 

Unfortunately, it ruins my algorithm completely.

can any one please mail me

gurteg.107 @ 10 Nov 2009 03:49 PM

can any one please mail me the solution of this problem????

I'm execute in Borland c++,

sureshthelegend @ 7 Dec 2009 09:19 PM

I'm execute in Borland c++, but i could not code for the above given languages,,,, can any one help me ???????

 

Though this may sound very

vikilive @ 10 Dec 2009 03:06 PM

Though this may sound very noobish, here i go. Im new to all this, gcc and stdin/stdout. Ive submitted my soultiona nd am getting a segmentation fault. When i executed it on my own machine (in gcc) by taking in inputs whenever required i could arrive at the desired results, which forces me to think that im making a mistake in taking in input. I will post my code segment wherin i take in input and any suggestions or tweaks would be greatly appreciated.

#include <stdio.h>
#include <string.h>
int main ()
{
int tc=0;
scanf("%d",&tc);  //take in test cases
while(tc--)  
{


scanf("%d",&count);  //take in word count
while(count--)
{
//code segment

}

return 0;
}

 

Sorry, the correct code for

vikilive @ 10 Dec 2009 03:09 PM

Sorry, the correct code for the above comment is as follows

 

#include <stdio.h>
#include <string.h>
int main ()
{
int tc=0;
scanf("%d",&tc);  //take in test cases
while(tc--)  
{


scanf("%d",&count);  //take in word count
while(count--)
{
//code segment

}


}
return 0;
}

 

 

Vignesh

can you plz let me know where

sumit.guha @ 27 Jan 2010 08:12 PM

can you plz let me know where m i going wrong??? it seems correct on my part...

a) You aren't reading input

triplem @ 28 Jan 2010 05:31 AM

a) You aren't reading input correctly. Read the FAQ.

b) Your code will fail on cases like aa bb.

Doesn't the problem reduce to

teremach @ 8 Feb 2010 03:34 PM

Doesn't the problem reduce to determining the existence of a Hamiltonian path (chain) in a directed graph? And the latter is an NPC problem.

The problem is solvable. That

triplem @ 9 Feb 2010 01:59 AM

The problem is solvable. That answers your question.

@stephen Plz help, i do not

vikrantsingh02 @ 10 Apr 2010 04:18 PM

@stephen

Plz help, i do not get my fault. how can i get a test case ? atleast here we can get test cases, its not a contest.

 

You could get test cases; you

triplem @ 11 Apr 2010 03:21 AM

You could get test cases; you could also just have someone tell you the solution. The hardest part of solving a problem is coming up with your own test cases - but in this case it isn't too hard to do yourself at all. There are only a few test cases that you need to check; be systematic and you'll find one your program fails on.

@

prajata @ 18 Jun 2010 08:52 PM

@ admins

http://www.codechef.com/viewsolution/270509 getting wrong answer for this. can you give me a hint??

@admins i m not getting the

nmsunny1 @ 18 Jun 2010 11:55 PM

@admins

i m not getting the test cases for which my prog is failing to generate the reqd output.....can u plz help me out

import java.io.BufferedReader;

import java.io.InputStreamReader;

 

class str

{

public static void main(String args[])throws Exception

{

BufferedReader q=new BufferedReader(new InputStreamReader(System.in));

int t=Integer.parseInt(q.readLine());

for(int i=0;i<t;i++)

{

int n=Integer.parseInt(q.readLine());

String s[]=new String[n];

for(int j=0;j<n;j++)

s[j]=q.readLine();

char c[]=new char[n];

char e[]=new char[n];

for(int k=0;k<n;k++)

{

c[k]=s[k].charAt(0);

e[k]=s[k].charAt(s[k].length()-1);

}

int f=0,g=0;

for(int l=97;l<=122;l++)

{

char d=(char)l;

int tmp1=0,tmp2=0;

for(int j=0;j<n;j++)

{

if(c[j]==d)

tmp1++;

if(e[j]==d)

tmp2++;

}

if(tmp1>0 || tmp2>0)

if(tmp1!=tmp2)

f++;

else

g++;

}

//System.out.println("f = "+f);

//System.out.println("g = "+g);

if(f>=2&&g==0 || f>2)

System.out.println("The door cannot be opened.");

else

System.out.println("Ordering is possible.");

}

}

}

Hi,   I have tried everything

johnsonjthomas @ 19 Jul 2010 10:06 AM

Hi,

 

I have tried everything with this problem, I have always got Runtime Error. The Id is 289432.Could you tell me for which test case am I getting this error?. I have even tried giving 2 test cases and in each test case, 100000 plates and each word contains 1000 letters. And this works perfectly on my system. Could you please help me out.

 

Thanks,

Johnson

Hi, I had not checked for one

johnsonjthomas @ 19 Jul 2010 10:15 AM

Hi,

I had not checked for one element test case and that gave me a segmentation fault, I rectified that but still getting the runtime error. Id of the rectified code is 289438

 

Thanks,

Johnson

i submitted the following

nikhil belchada @ 28 Jul 2010 12:12 PM

i submitted the following code.I m getting 'Tine Limit Exceede'.what is the problem?

using System;


class MainClass
{
public static void Main (string[] args)
{

int b=int.Parse(Console.ReadLine());
for(int i=0;i<b;i++)
{

int x=int.Parse (Console.ReadLine());
string [] input=new string [x];
int check=0;
for(int j=0;j<x;j++)
{

input[j]=Console.ReadLine();
}
for(int k=0;k<x;k++)
{
for(int l=0;l<x;l++)
{
if(l!=k)
{
if(input[k].Substring(0,1)==input[l].Substring(input[l].Length -1,1))
{
check++;
break;
}
else if(input[k].Substring(input[k].Length -1,1)==input[l].Substring(0,1))
{
check++;
break;
}
}

}


}
if(check==x)
{
Console.WriteLine("Ordering is possible");
check=0;
}
else
{
Console.WriteLine("The door cannot be opened");
check =0;
}

}

}
}


:( working pefectly on my

satbirsoni @ 2 Aug 2010 09:37 PM

:( working pefectly on my system..On submission i am getting runtime error.. :(

some body else , Please try

satbirsoni @ 2 Aug 2010 09:40 PM

some body else , Please try below code any find any bug if there ..thx

 

#include<iostream>
#include<vector>

using namespace std;
typedef std::vector<int> vecType;
const std::string NO("The door cannot be opened.n");
const std::string YES("Ordering is possible.n");

void func(char & input,vecType  & allChars,bool first=true)
{
int a;

switch(input)
{
case 'a':
{
a=0;
break;
}
case 'b':
{
a=1;
break;
}
case 'c':
{
a=2;
break;
}
case 'd':
{
a=3;
break;
}
case 'e':
{
a=4;
break;
}
case 'f':
{
a=5;
break;
}
case 'g':
{
a=6;
break;
}
case 'h':
{
a=7;
break;;
}
case 'i':
{
a=8;
break;;
}
case 'j':
{
a=9;
break;
}
case 'k':
{
a=10;
break;
}
case 'l':
{
a=11;
break;
}
case 'm':
{
a=12;
break;
}
case 'n':
{
a=13;
break;
}
case 'o':
{
a=14;
break;
}
case 'p':
{
a=15;
break;
}
case 'q':
{
a=16;
break;
}
case 'r':
{
a=17;
break;
}
case 's':
{
a=18;
break;
}
case 't':
{
a=19;
break;
}
case 'u':
{
a=20;
break;
}
case 'v':
{
a=21;
break;
}
case 'w':
{
a=22;
break;
}
case 'x':
{
a=23;
break;
}
case 'y':
{
a=24;
break;
}
case 'z':
{
a=25;
break;
}
}
if(first)
allChars[a]=allChars[a]++;
else
allChars[a]=allChars[a]--;




}
int main()
{
short numTest;
vector<bool> results(numTest,true);
cin>>numTest;
for(int i=0;i<numTest;i++)
{
int numInput;
cin>>numInput;
vecType vec(26,0);
for(int j=0;j<numInput;j++)
{
std::string inString;
cin>>inString;
func(inString[0],vec,true);
func(inString[inString.size()-1],vec,false);          
}
int minusOne=0;
int plusOne=0;
for(vecType::iterator itr=vec.begin(); itr!=vec.end();itr++)
{
if((*itr!=1) && (*itr!=-1) && (*itr!=0))
{
results[i]=false;
break;
}
if(*itr==-1)
minusOne++;
if(*itr==1)
plusOne++;
if(minusOne> 1 || plusOne>1)
{
results[i]=false;
break;
}



}

}

for( int i=0;i<numTest;i++)
{
(results[i])?(cout<<YES):(cout<<NO);
}

return 0;
}

  Can someone tell me for

wenugopal @ 15 Aug 2010 06:22 PM

 

Can someone tell me for which test case is my program failing ?

my code works for test cases

jayesh02 @ 27 Aug 2010 01:34 AM

my code works for test cases i can think of, the test case on the discussion forum also works fine......please help!!!!!

Ive got really tired....plz

jayesh02 @ 28 Aug 2010 02:03 AM

Ive got really tired....plz give me a hint. I cant find where my sol fails?????????????????????

@Admin:Would it be possible

prakharprakhar @ 1 Dec 2010 03:37 PM

@Admin:Would it be possible to provide links to the algorithms which might be used for the problem. It might help in learning things better.

@Prakhar: Solutions to these

admin @ 2 Dec 2010 05:10 PM

@Prakhar: Solutions to these problems are visible at the top right hand corner of the page you can take a look at them to help you better understand the problem.

Plzz can any 1 help me wid

krishna1990 @ 12 Dec 2010 07:24 PM

Plzz can any 1 help me wid this solution http://www.codechef.com/viewsolution/398485 I couldnot find out any test case where it is going wrong :(

@admin I really tried a lot I

krishna1990 @ 12 Dec 2010 07:26 PM

@admin I really tried a lot I could not find plzzz plzz help me :(

hey got it got it :) :) have

krishna1990 @ 12 Dec 2010 07:43 PM

hey got it got it :) :) have done a silly mistake :P :(

hey again I got an error plzz

krishna1990 @ 13 Dec 2010 01:02 PM

hey again I got an error plzz can any 1 help me by giving a test case where my code fails :( :(

u can view my code here

krishna1990 @ 13 Dec 2010 01:02 PM

u can view my code here http://www.codechef.com/viewsolution/398485

Hi Steveee/Admin can you guys

ChunChun @ 20 Dec 2010 12:53 PM

Hi Steveee/Admin

can you guys look at http://www.codechef.com/viewsolution/403898 and give me an hint to where I might be wrong. Help is appreciated

http://www.codechef.com/views

ChunChun @ 20 Dec 2010 07:30 PM

http://www.codechef.com/viewsolution/404079

can anyone please help me on where it might be failing, i've been struggling for the whole day. Please help

Can the ADMINS please help

ChunChun @ 24 Dec 2010 03:21 PM

Can the ADMINS please help me. My code gives wrong answer

http://www.codechef.com/viewsolution/406396

Got it don't bother :|

ChunChun @ 24 Dec 2010 11:52 PM

Got it don't bother :|

my C++ code always shows

preethi_murali @ 15 Apr 2011 05:28 PM

my C++ code always shows compile time error!! could someone pls tell me wat's wrong wid ma code??also can u tell me if my logic is alright??

#include<iostream>
#include<stdlib.h>
#include<stdio.h>

main()
{
char str[50][1000],l[1000],s[1000];
int n,i,j,flag=0,cnt=0,ct=0,t;
cin>>t;
while(t--)
{
cout<<"Enter no of strings"<<endl;
cin>>n;
for(i=0;i<n;i++)
gets(str[i]);
for(i=0;i<n;i++)
 for(j=0;;j++)
 {
  if(j==0)
  s[i]=str[i][j];
  if(str[i][j]=='')
  {
  l[i]=str[i][j-1];
  break;
  }
 }
for(j=0;j<n;j++)
{
 for(i=0;i<n;i++)
  if(l[j]==s[i])
  {
  ct++;
  flag=1;
  }
 if(flag==0)
 cnt++;
}
if(cnt>1 || ct<(n-1))
{
 cout<<"the door cannot be opened"<<endl;
 exit(0);
}
else
cout<<"ordering is possible"<<endl;
}
return 0;
}

The problem statement says

sergico @ 30 Jun 2011 02:37 AM
The problem statement says that 1<= N <= 100000 If only 1 plate is available that means the door can be opened or not? Thanks /sergico

@sergico: N=1 Ordering is

imnewcoder @ 1 Jul 2011 01:27 AM
@sergico: N=1 Ordering is possible.

worked perfectly on my comp.

jchunchu @ 3 Sep 2011 11:12 AM
worked perfectly on my comp. on submitting showing runtime error. my program is import java.io.*; class codechef5 { public static void main(String arg[]) throws Exception { DataInputStream x=new DataInputStream(System.in); int i,i1,n,k,j; int p=Integer.parseInt(x.readLine()); String s[]; char c1[],c2[]; for(i1=0;i1

=n-1) { System.out.println("ordering is possible"); break;} } }} if(k

import java.io.*; class

jchunchu @ 3 Sep 2011 11:14 AM
import java.io.*; class codechef5 { public static void main(String arg[]) throws Exception { DataInputStream x=new DataInputStream(System.in); int i,i1,n,k,j; int p=Integer.parseInt(x.readLine()); String s[]; char c1[],c2[]; for(i1=0;i1

=n-1) { System.out.println("ordering is possible"); break;} } }} if(k

would an O(n^2) solution do?

nitishupreti @ 28 Sep 2011 10:44 PM
would an O(n^2) solution do?

@admins Please atleast check

flexicoder @ 8 Oct 2011 01:13 PM
@admins Please atleast check if I am not outputing the answer incorrectly or if I am making any similar silly mistake......i.e is my answer correct for simple cases and including the 'aa' type cases... works correctly on my system....i dnt thnk m missing any test cases....plz help

http://www.codechef.com/views

flexicoder @ 8 Oct 2011 01:20 PM
http://www.codechef.com/viewsolution/692693 plz help !...am i missing smthng here..?

@admin Please can u provide

bharatv @ 31 Dec 2011 11:33 AM
@admin Please can u provide with an example of input no N which is provided as the most largest input.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com