Wrong Answer,Can't find out whats wrong in my code - c++

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;
}
}

Related

Character counter returning incorrect value for char[n] array

I am writing a C++ program for homework, and it needs to count the characters in a char arr[n] string. However, my counter keeps returning the wrong values. I have looked through other answers to similar questions, however, they are not specific to C++ and none of the answers explain the value I am getting.
#include<iostream>
using namespace std;
#include<string.h>
#include <stdlib.h>
class Counter
{
public:
char word[20];
int totChar{ 0 };
void setWord(char word)
{
this->word[20] = word;
}
void setCount(int totChar)
{
this->totChar = totChar;
}
int getLength()
{
return totChar;
}
void charCount()
{
int n = 0;
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] != '\0')
{
n++;
}
}
setCount(n);
}
};
int main()
{
char text[20];
cout << "Enter the string:" << endl;
cin >> text;
Logic input;
input.setWord(text[20]);
input.charCount();
// input.resetWord();
cout << input.getLength();
}
So it seems you haven't figured out how arrays and C strings work in C++ yet.
void setWord(const char* word)
{
strcpy(this->word, word);
}
and
Logic input;
input.setWord(text);
Your code is a bit weird, I guess you are just experimenting, but I think those two changes should make it work.

Problem in taking input while running a c++ program

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.

scanf for both character and integer

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;
}

Runtime error "SIGCONT" while finding lapindromes using c++

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;
}

C++ (SPOJ - POL-polowa) - problem with compilation

I have problem with making correct code for this task in SPOJ https://pl.spoj.com/problems/POL/.
When I wrote all what I needed, program worked correctly. But when I tried to convert it in function, I have on ideone.com problem like this -> double free or corruption (out). Can anybody help me please ? What I am doing wrong ? I am beginner and I realize that can be very trivial.
#include <iostream>
using namespace std;
int polowa()
{
int t;
cin>>t;
string slowa[100]={};
string nowe_slowa[100]={};
for (int i=0;i<t;i++)
{
cin>>slowa[i];
}
for (int i=0;i<t;i++)
{
int k=slowa[i].length()/2;
nowe_slowa[i]=slowa[i].substr(0,k);
cout<<nowe_slowa[i]<<endl;
}
}
int main()
{
polowa();
return 0;
}
int polowa() promises to return an int, but the return keyword is conspicuously absent from the function.
Looking over the code, you never use more than one element at a time. Odds are good you could rewrite this function to use no arrays and only one loop. – user4581301
You don't have to separate reading input and print output. Arrays are not needed. Proposed implementation:
#include <iostream>
#include <string>
int main()
{
unsigned tests;
std::cin >> tests;
while (tests--)
{
std::string word;
std::cin >> word;
std::cout << word.substr(0, word.length() / 2) << "\n";
}
}
According to Dessus proposition I wanted to make this code as a function called in int main(). But SPOJ can't accept me this code (when i am compiling this in code blocks, everythings okay).
#include <iostream>
#include <string>
std::string nowe_slowo(int t)
{
while (t--)
{
std::string word;
std::cin >> word;
std::cout<<word.substr(0, word.length() / 2)<<"\n";
}
}
int main()
{
unsigned tests;
std::cin >> tests;
nowe_slowo(tests);
}