You are not logged in. Please login at www.codechef.com to post your questions!

×

Please Explain the approach for this easy problem?

Problem Link-https://www.codechef.com/problems/DISTCODE how to use 2d boolean array?

asked 20 May '17, 14:30

vivek96's gravatar image

2★vivek96
518219
accept rate: 7%

edited 20 May '17, 14:31


An easy approach could be to insert all 2 letter sub-strings into a set and then just prining the size of the set (since a set stores distinct elements). If you don't know about sets, I'd suggest you learn about it, it's quite useful in CP.

link

answered 20 May '17, 14:41

abdullah768's gravatar image

6★abdullah768
2.4k420
accept rate: 17%

i know about sets in stl,but in this question i have to generate all substrings and check thier length and insert into set? im also familar with collection frameworks in java,but please explain the approach? what about 2d array of 26*26?

(20 May '17, 14:46) vivek962★

No, all you need to do is insert contiguous sub-strings that are of size 2. you can start a loop from 0 to less than n-1 and then insert sunstring(s,i,i+1). After the loop, print the size of the set.

(20 May '17, 15:11) abdullah7686★

You can use a Boolean array B of size 26*26, each row and column representing an alphabet from A(0) to Z(25).

Initialize everything to zero.

run a loop from 0 to less than n-1

make B[s[i]-'A'][s[i+1]-'A']=1

Note: A subtracted to convert the value of s[i] to int( 0 for A and 25 for Z)

(20 May '17, 15:20) abdullah7686★

what is logic behind this 26*26 array?

(20 May '17, 15:33) vivek962★

Each row denotes the first letter of the 2 lettered sub string ad column denotes the second letter. Eg: b[1][1] represents the sub string BB. So, once BB is encountered, you set B[1][1] to 1. If BB is encountered again, you set B[1][1] to 1 again, which has no effect. Hence BB will be counted only once, regardless of how many times it occurred.

(20 May '17, 15:46) abdullah7686★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,673
×2,646
×31

question asked: 20 May '17, 14:30

question was seen: 449 times

last updated: 20 May '17, 15:46