SPOJ is giving WA for this solution of ROCK [on hold]











up vote
-4
down vote

favorite












I am solving ROCK problem on SPOJ. I dont know why I am getting WA here. Tried many testcases.



#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define UB upper_bound
#define LB lower_bound

int main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int t;
cin>>t;
while(t--)
{
int n,i,j;
string s;
map <int,int> m;
cin>>n>>s;
int a[n+1];
a[0]=0;
for(i=0;i<n;i++)
{
int x=(s[i]=='1') ? 1 : -1;
a[i+1]=a[i]+x;
}

for(i=1;i<=n;i++)
{
m[a[i]]=i;
}
int ans=0,p=0;
for(i=1;i<=n;i++)
{
int r=0;
int j=i;
while(j<=n)
{
int p=a[j-1];
int x=m[p+1];
if(x<j)
{
j++;
continue;
}
r+=(x-j+1);
j=x+1;
}
ans=max(ans,r);
}
cout<<ans<<"n";
}
return 0;
}


Please suggest where my code is wrong. Thanks










share|improve this question







New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    I'm sorry we don't fix broken code. Please see the help center for more information.
    – bruglesco
    12 hours ago






  • 3




    Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
    – Mast
    12 hours ago






  • 2




    For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
    – Mast
    12 hours ago















up vote
-4
down vote

favorite












I am solving ROCK problem on SPOJ. I dont know why I am getting WA here. Tried many testcases.



#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define UB upper_bound
#define LB lower_bound

int main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int t;
cin>>t;
while(t--)
{
int n,i,j;
string s;
map <int,int> m;
cin>>n>>s;
int a[n+1];
a[0]=0;
for(i=0;i<n;i++)
{
int x=(s[i]=='1') ? 1 : -1;
a[i+1]=a[i]+x;
}

for(i=1;i<=n;i++)
{
m[a[i]]=i;
}
int ans=0,p=0;
for(i=1;i<=n;i++)
{
int r=0;
int j=i;
while(j<=n)
{
int p=a[j-1];
int x=m[p+1];
if(x<j)
{
j++;
continue;
}
r+=(x-j+1);
j=x+1;
}
ans=max(ans,r);
}
cout<<ans<<"n";
}
return 0;
}


Please suggest where my code is wrong. Thanks










share|improve this question







New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    I'm sorry we don't fix broken code. Please see the help center for more information.
    – bruglesco
    12 hours ago






  • 3




    Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
    – Mast
    12 hours ago






  • 2




    For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
    – Mast
    12 hours ago













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I am solving ROCK problem on SPOJ. I dont know why I am getting WA here. Tried many testcases.



#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define UB upper_bound
#define LB lower_bound

int main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int t;
cin>>t;
while(t--)
{
int n,i,j;
string s;
map <int,int> m;
cin>>n>>s;
int a[n+1];
a[0]=0;
for(i=0;i<n;i++)
{
int x=(s[i]=='1') ? 1 : -1;
a[i+1]=a[i]+x;
}

for(i=1;i<=n;i++)
{
m[a[i]]=i;
}
int ans=0,p=0;
for(i=1;i<=n;i++)
{
int r=0;
int j=i;
while(j<=n)
{
int p=a[j-1];
int x=m[p+1];
if(x<j)
{
j++;
continue;
}
r+=(x-j+1);
j=x+1;
}
ans=max(ans,r);
}
cout<<ans<<"n";
}
return 0;
}


Please suggest where my code is wrong. Thanks










share|improve this question







New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am solving ROCK problem on SPOJ. I dont know why I am getting WA here. Tried many testcases.



#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define mp make_pair
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define UB upper_bound
#define LB lower_bound

int main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int t;
cin>>t;
while(t--)
{
int n,i,j;
string s;
map <int,int> m;
cin>>n>>s;
int a[n+1];
a[0]=0;
for(i=0;i<n;i++)
{
int x=(s[i]=='1') ? 1 : -1;
a[i+1]=a[i]+x;
}

for(i=1;i<=n;i++)
{
m[a[i]]=i;
}
int ans=0,p=0;
for(i=1;i<=n;i++)
{
int r=0;
int j=i;
while(j<=n)
{
int p=a[j-1];
int x=m[p+1];
if(x<j)
{
j++;
continue;
}
r+=(x-j+1);
j=x+1;
}
ans=max(ans,r);
}
cout<<ans<<"n";
}
return 0;
}


Please suggest where my code is wrong. Thanks







algorithm programming-challenge dynamic-programming






share|improve this question







New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 hours ago









Invictus31

11




11




New contributor




Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Invictus31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, Heslacher, 200_success, Toby Speight, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    I'm sorry we don't fix broken code. Please see the help center for more information.
    – bruglesco
    12 hours ago






  • 3




    Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
    – Mast
    12 hours ago






  • 2




    For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
    – Mast
    12 hours ago














  • 1




    I'm sorry we don't fix broken code. Please see the help center for more information.
    – bruglesco
    12 hours ago






  • 3




    Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
    – Mast
    12 hours ago






  • 2




    For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
    – Mast
    12 hours ago








1




1




I'm sorry we don't fix broken code. Please see the help center for more information.
– bruglesco
12 hours ago




I'm sorry we don't fix broken code. Please see the help center for more information.
– bruglesco
12 hours ago




3




3




Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
– Mast
12 hours ago




Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. Please take a look at the help center.
– Mast
12 hours ago




2




2




For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
– Mast
12 hours ago




For your next question, don't forget to include what the code is supposed to do and the testcases you ran against it.
– Mast
12 hours ago















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Mont Emei

Province de Neuquén

Journaliste