PLEASE CHECK MY CODE ONCE:
My almost all custom inputs are working well and giving out correct solutions, but while SUBMITTING its providing WRONG ANSWER
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s , t1;
cin >> s;
int j , n = s.length();
char char_array[n + 1];
strcpy(char_array, s.c_str());
int t , i;
cin >> t;
for( i = 0 ; i < t ; i++ )
{
cin>>t1;
int k = t1.length();
char char_array2[k + 1];
strcpy( char_array2, t1.c_str() );
int m , a = 0;
for( j = 0 ; j < = k ; j++ )
{
for( m = 0 ; m < = n ; m++ )
{
if( char_array2[j] == char_array[m] )
{
a++;
}
}
}
if(a!=n+1)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
In the above code, there’s a line marked “This line”. If I am using getline, my code was not accepted, but when I used cin, my code was accepted. Any idea why this happened?
PLEASE TELL THE MISTAKE . IT IS PARTIALLY CORRECT
a=input()
p=set(a)
z=list(p)
z.sort()
c="".join(sorted(z))
b=int(input())
for i in range(b):
d=input()
x=set(d)
y=list(x)
y.sort()
g="".join(sorted(y))
if(c==g):
print(“Yes”)
else:
print(“No”)
What if a = “xyz” and g = “xy”? It should print “Yes” because “xy” contains characters he can read. But in your program, it would print “No”. Your a and g doesn’t have to be equal for him to be able to read it. Instead, characters in g have to be a subset of characters in a.
// Online C compiler to run C program online
#include <stdio.h>
#include<string.h>
int main() {
// Write C code here
char str[27];
scanf("%s",&str);
long long int n=0;
scanf("%lld",&n);
while(n--)
{
char str2[13];
long long int count=0;
scanf("%s",&str2);
if(strlen(str2)==1)
{
for(int i=0;i<27;++i)
{
if(i==26)
{
printf("No\n");
}
if(str2[1]==str[i])
{
printf("Yes\n");
break;
}
}
}
else
{
for(long long int i=0;i<13;++i)
{
for(long long int j=0;j<27;++j)
{
if(str2[i]>=97&&str2[i]<=122)
{
if(str2[i]==str[j])
{
count++;
break;
}
else if(str2[i]!=str[j])
{
continue;
}
}
else
{
continue;
}
}
}
if(count==strlen(str2))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return 0;
}
It works fine… , for example, let us consider the input:
dog
1
o
it gives correct results for ‘o’, which is ofcourse “Yes”
i have read this whole editorial and edited the code according to what you guys said. I could not think of a better code than this(i’m only 12 years old)
please help me out.
it is showing “wrong answer”…
Hey @iron102 ,
Your code is correct only problem is on the for loop.You always run a loop for 27 for str and 13 for str2 here only your code is giving wrong even If input string is of size 5 for str but your code will still run for 27 making the logic wrong. Just put strlen(str) in place of 27 in every for loop and strlen(str2) in place of 13. Also make sure to handle the case when strlen(str2)==1 with this modification.
Here is your code with modification.
#include <iostream>
#include<string.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
char str[27];
scanf("%s", &str);
long long int n = 0;
scanf("%lld", &n);
while (n--)
{
char str2[13];
long long int count = 0;
scanf("%s", &str2);
if (strlen(str2) == 1)
{
for (int i = 0; i < strlen(str) + 1; ++i)
{
if (i == strlen(str))
{
printf("No\n");
break;
}
if (str2[0] == str[i])
{
printf("Yes\n");
break;
}
}
}
else
{
for (long long int i = 0; i < strlen(str2); ++i)
{
for (long long int j = 0; j < strlen(str); ++j)
{
if (str2[i] >= 97 && str2[i] <= 122)
{
if (str2[i] == str[j])
{
count++;
break;
}
else if (str2[i] != str[j])
{
continue;
}
}
else
{
continue;
}
}
}
if (count == strlen(str2))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return 0;
}
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
static void display(int[] x)
{
for(int i=0; i<x.length; i++)
{
System.out.print(x[i]+" ");
}
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int N = sc.nextInt();
sc.nextLine();
int l = s.length();
int[] s1 = new int[26];
for(int i=0; i<26; i++)
{
s1[i]=0;
}
for(int i=0; i<l; i++)
{
s1[(int)s.charAt(i)-'a'] = 1;
}
String r = "Yes";
for(int i=0; i<N; i++)
{
String w = sc.nextLine();
l=w.length();
for(int j=0; j<l; j++)
{
if(s1[(int)w.charAt(j)-'a']!=1)
{
r="No";
break;
}
}
System.out.println(r);
}
// display(s1);
}