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
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » January 2010 (Contest XII) » Wildcard Matching

Wildcard Matching

Problem code: L2

  • All Submissions

All submissions for this problem are available.

Problem Statement Updated.

Johnny is the author of the famous open source software ByteDict (Bytelandian electronic dictionary).

Being a fan of the word game Scrabble, Johnny would like to make ByteDict useful for Scrabble training. Therefore, he has decided to implement the "Wildcard Matching" feature.

Wildcard matching allows ByteDict's users to search for words matching a given wildcard pattern. A wildcard pattern may include the characters '*' or '?' in addition to the characters of the alphabet. A '*' matches any sequence of characters (including the empty sequence) and a '?' matches any single character.

To be more precise, a wildcard pattern is matched with a word if we can replace each '?' with an alphabet character and each '*' with a sequence of alphabet characters so that after replacement, the pattern becomes identical to the word.

Now, as usual, please help Johnny implement the feature!

Input

The first line contains a number t (about 2) which is the number of test cases. Then t test cases follow. Each test case has the following form.

The first line contains two integers N and Q (1 <= N <= 55000, 1 <= Q <= 300) which are the number of words in the dictionary and the number of wildcard queries.

Each line in the next N lines contains a word from the dictionary. The alphabet consists of lower and uppercase characters as well as digits.

Each line in the next Q lines contains a wildcard pattern.

The length of each word does not exceed 25 and the length of each wildcard pattern does not exceed 5.

Each test case is separated by a blank line.

Output

For each wildcard query print a line containing the query itself and the number of words in the dictionary that match the query.

Print a blank line after each test case's output.

Example

Input:
2
1 7
abc
abc
cba
a??
??a
*?a
***
**a

7 4
spoj
codechef
uva
tju
onlinejudge
Acmicpc
Worldcup2010
*a*
sp?*
*c*
uvc

Output:
abc 1
cba 0
a?? 1
??a 0
*?a 0
*** 1
**a 0

*a* 1
sp?* 1
*c* 3
uvc 0


Author:
Date Added: 15-12-2009
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

The less than symbols are

triplem @ 1 Jan 2010 03:27 PM

The less than symbols are broken. Again.

It also says that the length of the wildcard pattern does not exceed 5, yet the test cases show otherwise. Please fix.

Can this please be fixed?

triplem @ 2 Jan 2010 09:37 AM

Can this please be fixed?

As Stephen indicated, '*c*'

jcomeau_ictx @ 2 Jan 2010 01:14 PM

As Stephen indicated, '*c*' does not match 'Worldcup2010' if '*' matches only up to 5 chars, therefore '*c*' should show '2' matches only.

Uh, no, it doesn't say

triplem @ 2 Jan 2010 01:46 PM

Uh, no, it doesn't say anything about how many characters * can match. Just that the length of the wildcard string is at most 5, whereas *?*?*?*? is of length 8.

The problem statement has

admin @ 2 Jan 2010 02:29 PM

The problem statement has been updated.

Argh... There seems to

ashutoshmehra @ 2 Jan 2010 04:06 PM

Argh... There seems to something fishy with the judge's output/checker. I've verified my program with large testcases locally, with grep, and it gives correct output. But the judge refuses to accept ;-) And no one else has been able to get an AC on this one either.

To restate the obvious: (1) the matching (of non-wildcards) should be case-sensitive (2) * matches any number (including 0 or greater than 5) characters (3) Both * and ? match any character from the sets [A-Z], [a-z], [0-9].

if the input is 1 1

sesha_giri_nit @ 2 Jan 2010 07:09 PM

if the input is

1

1 1

abaca

*ac*

 

what will be the output

*ac* 1

or

*ac* 0

@Giri: In your example, do

ashutoshmehra @ 2 Jan 2010 07:18 PM

@Giri: In your example, do you have a line of reasoning that explains the output being 0? I would say it should be 1 (but then my solution isn't AC'ed).

ohh... :hangs head in

jcomeau_ictx @ 2 Jan 2010 08:37 PM

ohh... :hangs head in shame:

Thanks, Stephen.

@Giri ... it should be 1 ...

sandeepkkothari @ 3 Jan 2010 11:04 AM

@Giri ... it should be 1 ...

Judge data seems faulty or

pr0ton @ 3 Jan 2010 12:31 PM

Judge data seems faulty or not conforming to the standards. I sent a checker and it got a runtime error. Admins please confirm

Ok, only the test-cases are

pr0ton @ 3 Jan 2010 12:37 PM

Ok, only the test-cases are more than 2 i think. Otherwise everything seems fine :)
Sorry :)

Yup, it doesn't say there

triplem @ 3 Jan 2010 01:04 PM

Yup, it doesn't say there will be exactly 2 test cases.

I agree with the earlier

triplem @ 4 Jan 2010 01:46 PM

I agree with the earlier comments, the judge appears to be faulty here.

Checking.

admin @ 4 Jan 2010 03:55 PM

Checking.

Judge appears to have been

pieguy @ 5 Jan 2010 05:56 AM

Judge appears to have been fixed.

Can a wildcard pattern have

pr0ton @ 5 Jan 2010 07:58 PM

Can a wildcard pattern have length of 0 ?

A wildcard pattern cannot

admin @ 5 Jan 2010 09:59 PM
A wildcard pattern cannot have length 0. The length is between 1 and 5

Admin, I have solved three

sppraveen @ 6 Jan 2010 11:42 PM

Admin,

I have solved three problems. I am unable to view my name in Indian rankings. Please check.My country is showing as 11.I would be happy to see my name in the rankings.

Yeah, we are looking into

admin @ 7 Jan 2010 12:13 AM

Yeah, we are looking into this. This should get solved tomorrow.

thanks a lot!

sppraveen @ 7 Jan 2010 12:25 AM

thanks a lot!

can any1 give me a worst case

vikrantsingh02 @ 7 Jan 2010 11:15 AM

can any1 give me a worst case input?

i thought

vvvvvvvvvvvvvvvvvvvvvvvvv repeated 55000 times and v*v*v repeated 300 times would be the worst case

Nobody will give you hints,

triplem @ 7 Jan 2010 11:27 AM

Nobody will give you hints, no.

I think

vikrantsingh02 @ 7 Jan 2010 01:10 PM

I think vvvvvvvvvvvvvvvvvvvvvvvvv is 25 characters long

so it will take 55000X300X25 units of time at min.

for more than 2 test cases it will take more than 2 secs?

Isn't the time limit less than what shud be?

eg:

                     int a=55000,b=300,i,j,c;

                     for(int test=0;test<3;test++)
                     for(i=0;i<b;i++)
                      for(j=0;j<a;j++)

                      for(int y=0;y<25;y++)

                       c=a+b;

* can be replace by a

msjaiswal @ 7 Jan 2010 01:53 PM

* can be replace by a sequence of characters as mentioned by the question. Can the sequence of characters be a null sequence also?

That is specifically

triplem @ 7 Jan 2010 02:29 PM

That is specifically mentioned in the problem statement.

Stephen can you clear my

vikrantsingh02 @ 7 Jan 2010 02:40 PM

Stephen can you clear my doubt?

I bet i only try to improve my programming skills , not cheat myself.

The time limit is 2 seconds.

triplem @ 7 Jan 2010 02:41 PM

The time limit is 2 seconds.

@Praveen Submit code for any

i0exception @ 7 Jan 2010 05:46 PM

@Praveen Submit code for any of the problems and this information should then be updated.

Ya done Admin. I am seeing it

sppraveen @ 7 Jan 2010 06:27 PM

Ya done Admin. I am seeing it now.Thanks

There seems to be blank lines

MichaelD @ 9 Jan 2010 03:41 PM

There seems to be blank lines in the input that could, but should not, be treated as words of length zero. Have I got that right?

Are there empty lines in

abhijith @ 10 Jan 2010 05:39 PM

Are there empty lines in input that should be processed ?

It looks like there are empty

MichaelD @ 10 Jan 2010 11:36 PM

It looks like there are empty lines that should NOT be processed. When I allowed for empty words (which the problem statement doesn't exclude) I got a wrong answer. Ignoring blank lines works.

Yup.. ignoring blank lines

abhijith @ 11 Jan 2010 09:35 AM

Yup.. ignoring blank lines worked..

@ admin .. even my name doesn't show up in the rank list

I believe it is showing up

admin @ 12 Jan 2010 03:42 PM

I believe it is showing up now. It takes a bit of time for the rank list to be updated after submission of a solution.

I m getting Wrong answer

msjaiswal @ 12 Jan 2010 06:13 PM

I m getting Wrong answer again and again and I am sure its because I m using "gets()". 
scanf() is too slow.

Can someone pls suggest me what shall I do to speedily read a sentence from input???

vvvvvvvvvvvvvvvvvvvvvvvvv

vikrantsingh02 @ 16 Jan 2010 01:09 PM

vvvvvvvvvvvvvvvvvvvvvvvvv repeated 55000 times and v*v*v repeated 300 times would be the worst case

I think vvvvvvvvvvvvvvvvvvvvvvvvv is 25 characters long

so it will take 55000X300X25 units of time at min.

for more than 2 test cases it will take more than 2 secs?

Isn't the time limit less than what shud be?

eg:

                     int a=55000,b=300,i,j,c;

                     for(int test=0;test<3;test++)
                     for(i=0;i<b;i++)
                      for(j=0;j<a;j++)

                      for(int y=0;y<25;y++)

                       c=a+b;

i downloaded the successful submission after the contest was over ,

that succesful submission gave more than 10 s to give solution for above input case.

my program takes 5-6 s for the above input case and same matching algo with some improvisation.

even so it shows TLE , PLZ tel me why?

Stephen ,, plz clarify . . . .

 

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
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 algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm 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 programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were 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 challenges 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 coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

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. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

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