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.
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)
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());
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-
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)
I modified your code a bit. PLease compare it with your original code.
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
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.
//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;