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

×

STONES - Editorial

1
1

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

This was the easiest problem of the contest. All one had to was to move over all characters is S and check if they were present in J as well. Checking could be done either by iterating over all character of J which takes time O(|J|) or by keeping an array/hashset for O(1) look up.

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

asked 09 Nov '12, 12:40

admin's gravatar image

0★admin ♦♦
17.4k347487515
accept rate: 36%

edited 09 Nov '12, 15:24


@yyash here's the working code.

I modified your code a bit. PLease compare it with your original code.

http://ideone.com/mrDnXl

And yes you don't have to use that count array to display the results of test cases all at once. You can output the results of each test case one by one. And try using ideone next time to share codes

:D

link

answered 25 Mar, 15:33

shraeyas's gravatar image

2★shraeyas
1.1k1213
accept rate: 10%

include<stdio.h>

include<string.h>

define MAX 200

int main() { int t,j,s,i; char je[100],st[100]; scanf("%d",&t); while(t) { int h[MAX]={0}; int c=0; scanf("%s",je); scanf("%s",st); j=strlen(je); s=strlen(st); for(i=0;i<j;i++) h[je[i]]=je[i]; for(i=0;i<s;i++) { if(st[i]==h[st[i]]) c++; } printf("%d\n",c); t--; } return 0; }

what is wrong with this answer i dont understand i tried it on my computer and got the answers but here i am getting wrong answer

link

answered 16 Feb, 21:04

rusifan's gravatar image

2★rusifan
1
accept rate: 0%

@rusfian

Your code works all fine EXCEPT for 1 category of corner case.

Try changing string length to 101 and see the magic :)

Here is the test case at str length of 100-

 pre< 
 input
        1
        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab   >
Output-

100

The reason that the error occurs is that the end of string is taken up by position c, so there is nothing to denote end of line or end of string, and hence it continues taking the other "aaaaaa...." as input of string 1.

This results in string 2 being an empty string, and for it the condition is always true.

Its an abnormal behaviour, and hence 1 extra character is always recommended.

(Tested code by asking it to print je[101]...je[110] and got the chars of next line in it. Hence the above theory)

link

answered 16 Feb, 21:49

vijju123's gravatar image

4★vijju123 ♦
11.3k1316
accept rate: 18%

edited 16 Feb, 22:07

import java.util.; import java.lang.;

class jewels { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc,i,len1,len2,j,k; tc = sc.nextInt(); int count[] = new int[tc]; for(i=0;i<tc;i++) { StringBuffer jewels = new StringBuffer(sc.next()); StringBuffer stones = new StringBuffer(sc.next());

        len1 = jewels.length();
        len2 = stones.length();
        for(j=0;j<len1;j++)
        {
            for(k=0;k<stones.length();k++)
            {
                if(stones.charAt(k)==jewels.charAt(j))
                {
                    count[i]++;
                    stones.deleteCharAt(k);
                    break;
                }
            }
        }
    }
    for(i=0;i<tc;i++)
        {
            System.out.println(count[i]);
        }
}

} Can someone tell me whats wrong with this?

link

answered 25 Mar, 15:01

yyash's gravatar image

2★yyash
1
accept rate: 0%

1

stones.deleteCharAt(k);

is this line really required? Try submitting without this, I guess this seems to be the problem

(25 Mar, 15:07) shraeyas2★

Yes, @shraeyas is right. I too don't feel that the line is required.

(25 Mar, 15:15) vijju123 ♦4★

Thank you. Misunderstood the logic of the question

(25 Mar, 15:30) yyash2★

@yyash

I believe that you don't have to delete the characters of stone. Meaning, multiple isntances of that stone occurring has to be counted. Your code fails in the following test cases-

Input
1
abc
aaa

Output
1

Expected Output-
3
link

answered 25 Mar, 15:14

vijju123's gravatar image

4★vijju123 ♦
11.3k1316
accept rate: 18%

Isn't the code giving compilation error?

(25 Mar, 15:23) shraeyas2★

Its actually because of how the code is pasted here by him. I had to do corrections to resume checking....you can say I got experience of this now XD (since its a problem with >75% of codes copy pasted here)

(25 Mar, 15:25) vijju123 ♦4★

plz check my code as i am getting all cases right

import java.util.Scanner;
class codechef
{
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
    int test=kb.nextInt();
    while(test-->0)
{
String s1=kb.next();
        String s2=kb.next();
        int ans=0;
        for(int i=0;i<s1.length();i++)
        {
            for(int j=i;j<s2.length();j++)
            {
                if(s1.charAt(i)==s2.charAt(j))
                ans++;
            }
        }
        System.out.println(ans);

``}

`}}

link

answered 28 May, 15:58

palash7563's gravatar image

2★palash7563
153
accept rate: 0%

If anyone who doesn't know much about hashset or any data structure , u can go do this ques by using simple brute force .U can go through my code. https://www.codechef.com/viewsolution/15487815 If u have any issue feel free to share.

link

answered 21 Sep, 21:06

fad_coder00000's gravatar image

3★fad_coder00000
11
accept rate: 0%

//what is the problem in this programme.

include <bits stdc++.h="">

include <string.h>

using namespace std; int main() { int t,i,k,a; string s,j; cin>>t;

while(t--)
{
    cin>>j>>s;
    int p=0;
    for(i=0;j[i]!='\0';i++)
    {
        for(k=0;s[k]!='\0';k++)
        {
            if(j[i]==s[k])
            {
                p++;
                s[k]='{';
                break;
            }
        }
    }

    cout<<p<<"\n";
}

return 0;

}

link

answered 07 Dec, 06:51

ssinghal17's gravatar image

2★ssinghal17
1
accept rate: 0%

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:

×12,344
×2,686
×20
×13

question asked: 09 Nov '12, 12:40

question was seen: 3,702 times

last updated: 07 Dec, 06:51