I was trying to solve lapindrome problem on codechef.
On my local ide the program ran as expected.
But while in codechef submission it is giving runtime error "SIGCONT".
#include<iostream>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
int t;
cin>>t;
do
{
string l;
cin>>l;
int c = 0;
for (int i = 0; i < (l.length()/2); i++)
{
if (l[i] == l[i+(l.length()/2)])
{
c++;
}
}
if (c == l.length()/2) {
printf("YES\n");
}
else
{
printf("NO\n");
}
t--;
} while (t!=0);
return 0;
}
First thing I would like to point out that your code is wrong because for the test case rotor your code is giving NO as an answer, it should be YES.
Why getting the wrong answer?
In question it is given, if there are odd number of characters in the string, we ignore the middle character and check for lapindrome.
So, you need to modify/delete the below code.
if (c == l.length()/2) {
printf("YES\n");
}
else
{
printf("NO\n");
}
and instead do this:
if (c == l.length()/2) {
//check for lapindrome
}
else
{
//remove middle character
//check for lapindrome
}
Why "SIGCONT" error?
This error occurs when you exceed the IDE's printing limit. For Codechef IDE, it’s around 2^16 Characters.
You can try printing the output directly in a file locally. (Thanks to black_truce for this).
My Code:
You can check below solution for reference.
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
string str,str1,str2;
cin>>str;
int len=str.length();
if(len%2==0){
str1=str.substr(0, len/2);
str2=str.substr(len/2, len);
}
else{
str1=str.substr(0, len/2);
str2=str.substr(len/2+1, len);
}
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
if(str1==str2) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
Related
I am referring to this https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=208 problem. The problem is easy and self-explanatory. I made 2 attempts out of which one was Accepted and other was given Wrong Answer verdict.
The code which got selected is below where I did char by char input and output,
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
int flag = 0;
while((c = getchar())!=EOF) {
if(c == '\"') {
if(flag) {
cout<<'\''<<'\'';
flag = 0;
}
else {
cout<<'`'<<'`';
flag = 1;
}
}
else {
cout<<c;
}
}
return 0;
}
Whereas as following is the code which got wrong answer, here I took a line as input at a time. To me it does the same thing, but wrong answer.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int flag = 0, c = 0;
while(getline(cin, s)) {
if(c) {cout<<'\n';} //don't print newline first time
c=1;
for(auto& x : s) {
if(x == '\"') {
if(flag) {
cout<<'\''<<'\'';
flag = 0;
}
else {
cout<<'`'<<'`';
flag = 1;
}
}
else {
cout<<x;
}
}
}
return 0;
}
I am not able to figure out what's the issue ?
It will be helpful if someone can point out the problem in the second version.
I have already done this question with basic string methods,I am trying this question with stacks but getting wrong answer even my all test cases working correctly.
question link :https://www.codechef.com/problems/COMPILER
I really want to learn DSA hardly,please help me out and also give some suggestion in my coding style(i think i write code in very bad manner).
here is my code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
string str;
cin>>str;
class Node{
public:
char value;
int indexvalue;
};
std::stack<Node> f;
int start=0;
int end=0;
int k=-1;
for(int i=0;i<str.size();i++){
char s = str[i];
if(i == 0){
if(s == '>'){
break;
}
}
if(s=='<'){
Node n;
n.value=s;
n.indexvalue=i;
f.push(n);
}
else{
if(!f.empty()){
Node tempnode=f.top();
f.pop();
int tempstart=tempnode.indexvalue;
int tempend=i;
int tempk=tempend-tempstart;
if(tempk>k){
k=tempk;
start=tempstart;
end=tempend;
}
}
}
}
cout<<k+1<<endl;
}
}
Hey I was solving a stack problem and i have written my logic for it but when i run my program .
And providing 3 as input.
(a+(b*c))+(d/e)
((())(()))
((((()
It is not taking input correctly .At the end of the 3 there is question mark symbol appearing .
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
using namespace std;
int main()
{
long int t;
cin>>t;
while(t--){
stack< pair<char,int> > stk1;
string s;
cin>>s;
// getline(cin, s);
vector<int> vect;
int i=0,k=1,l=1;
while(s[i] != '\0' ){
if(s[i]=='('){
stk1.push( {'(' , k} );
vect.push_back(k);
k++;
}
else if( s[i]== ')'){
if(stk1.empty()){
vect.push_back(l);
l++;
}else{
vect.push_back( stk1.top().second);
stk1.pop();
}
}
i++;
}
for(int z=0;z<vect.size();z++){
cout<<vect[z]<<" ";
}
cout<<"\n";
}
return 0;
}
I was using DEV C++ in that only this error was comming not in code blocks .
so there was saome problem in dev c++ only.
I am trying to take input character and integer both. But when i use cin>>ch>>val; for taking input,it works.But Using scanf("%c%d",&ch,&val);,it shows me run time error.What can i do to get rid of this problem? I want to use scanf for faster input.
Here is my partial code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int q;
scanf("%d",&q);
while(q--)
{
char ch;
int val,in;
//cin>>ch>>val;
scanf("%c %d",&ch,&val);
in=val;
if(ch=='a'){
//scanf("%d",&val);
//update(1,0,m,++indx,val);
printf("First Case\n");
}else{
//si(in);
//if(in>tree[1]) printf("none\n");
//else query(1,0,m,in);
printf("Second Case\n");
}
}
}
I ran this code! It works fine for me. So probably you're using scanf in C++ and forgot to include or maybe it's something else with your code.
Can you share the full code. There is no issue with scanf() function as you have mentioned. You can take multiple inputs like scanf(%d%d%c%c,&a,&b,&c,&d);
#include <stdio.h>
int main()
{
printf("Hello World");
int q=1;
while(q--)
{
char ch;
int val,in;
scanf("%c%d",&ch,&val);
in=val;
if(ch=='a'){
printf("%d",val);
}else{
printf("%d",val);
}
}
return 0;
}
I've attempted to write a code that checks whether or not a string is a palindrome. Here is the code:
#include <iostream>
#include <string>
using namespace std;
bool pal(string str)//This block of code checks if input string is a palindrome
{
bool valid;
int i;
for (i = 0; i < str.length(); i++)
{
if (str[-i] == str[i])
{
valid = true;
}
else
{
valid = false;
}
}
return valid;
}
int main()
{
string s;
cin >> s;
if (!pal(s))
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
return 0;
}
Currently I am getting "Debug Assertion Fail" error.
str[-i] == str[i]
is a problem since negative indices are not valid indices in C++.
You need to change the strategy a little bit.
bool pal(string str)
{
int i = 0;
int j = str.length() - 1;
for ( ; i < j; ++i, --j)
{
if (str[i] != str[j])
{
// No need for any more checks.
return false;
}
}
// If we come here, the string is a palindrome.
return true;
}
C++ Provides us with an inbuilt function reverse() which can be used to reverse the Input string and compare it with un reversed string and print the output. The code goes as follows.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string str;
cin>> str;
string rev;
rev = str;
reverse(str.begin(), str.end()); // string reverse operation
if(rev == str){
cout<<"YES"<<endl; // Prints "Yes" if string is palindrome
}else{
cout<<"NO"<<endl; // Prints "No" if string is not palindrome
}
return 0;
}