XYSTR - Editorial

The runtime error that you are getting is SIGSEGV a.k.a Segmentation Fault. In layman terms, you’re accessing out of bounds. In C/C++ arrays are 0 indexed, so for A[n] you can access A[0]…A[n-1]. But A[n] throws a runtime error. It might’ve run smoothly in your compiler because accessing A[n] is undefined behavior and it’s different for different compilers.

A Simple Fix

Write your for loop as

for(int i = 0; i <n; i++)
{
    // your code
}
Or

Declare the array as
int ar[n + 1]

I strongly suggest the first fix.

[EDIT]: Also, you’re declaring ar[n] before taking the input! Before taking the input for n, the variable will be initialize with a junk value (if nothing is specified). You have to initialize your array after taking input for n. I guess this is why it ran smoothly in your compiler. Generally junk values are like 4576 or something. So for small inputs it’s fine as you have declared an array of size 4576. But when the given input to n is greater than the junk value, it throws an error.

1 Like

Thank you so much :blush:. You taught me something new today.

1 Like

Input :
1
xxxyyyy

expected output :
1
(their is only one pair of xy)

Your output :
3

1 Like

Can Anybody tell me what is wrong with this approach?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t–)
{
string s;
cin >> s;
int n = s.size();
int i,count=0;
for(i=0;i<n;i++)
{
if((s[i+1] - s[i]) == 1 || (s[i+1] - s[i] == -1))
{
count++;
i+=2;
}
else
{
i++;
count == count;
}
}
cout << count << endl;
}

return 0;

}

def count_substring(string, sub_string):
return (sum([1 for i in range(0, len(string) - len(sub_string) + 1) if (string[i:(len(sub_string)+i)] == sub_string)]))
ans=[]
for j in range(int(input())):
string = input().strip()
sub_string = “xy”
count = count_substring(string, sub_string)
ans.append(count)
print(*ans, sep = ‘\n’)

what is wrong here

3
xy
1
x
1
0
y
1
0
0

output error.
try this.

def count_substring(string, sub_string): return (sum([1 for i in range(0, len(string) - len(sub_string) + 1) if (string[i:(len(sub_string)+i)] == sub_string)]))
for j in range(int(input())): print ( count_substring(input().strip(), "xy") )

this is wrong ans

#include <bits/stdc++.h>

#define ll long long
#define ln "\n"
#define fastio ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define mod 1e9 + 7
#define rep(i, n) for(int i=0; i<n; i++)

using namespace std;

void solve()
{
string s;
cin>>s;
int cnt=0, last=-1;
for(int i=1; i<s.length(); ++i) {
    if(s[i]=='x' && s[i-1]=='y' && i!=last+1){
        cnt++;  
        last = i;
        continue;
    }
    if(s[i]=='y' && s[i-1]=='x' && i!=last+1){
        last = i;
        cnt++;
    }
    
}
cout << cnt << ln;

}

int main()
{
fastio;

int t; cin>>t;
while (t--)
    solve();
}

can anyone tell me why i am getting WA ??

#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
char a[100000];
cin>>a;
int c=0;
for(int i=1;a[i]!=’\0’:wink:
{
if(a[i-1]!=a[i])
{ c++;
i=i+2;
}
else
i++;
}
cout<<c<<endl;
}
}.

can anyone tell why i am getting WA

#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
char a[100000];
cin>>a;
int c=0;
for(int i=1;a[i]!=’\0’:wink:
{
if(a[i-1]!=a[i])
{ c++;
i=i+2;
}
else
i++;
}
cout<<c<<endl;
}
}

The logic seems to be correct. Move from index = 0 to index = n - 2 and see if pairs can be formed.

If yes(pairs can be formed), index should increase by 2.
If no(pairs cannot be formed), index increases by 1.

@cipher2323

#include<bits/stdc++.h>

using namespace std;

int main()

{

  int tastcase;

  cin>>tastcase;

  

  while(tastcase--)

  { 

      int count=0;

      string s;

     stack<int>a;

      cin>>s;

      for(int i=0;i<s.length();i++)

      {

          if(a.empty())

          {

              a.push(s[i]);

               continue;

          }

          

          if(a.top()!=s[i])

            {

                count++;

                a.pop();

            }

            else

            { 

                a.push(s[i]);

            }

      }

      cout<<count<<endl;

  }

}
tast case pass but answer slow worng
can anyone help me?

DP is an overkill man , simple iteration does the work!

#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define pii pair<int,int>
#define pb push_back
#define pp pop_back
#define vi vector<int>
using namespace std;

void solve(){
 string s;
 cin>>s;
 int ans=0;
 for(int i=0;i<s.length()-1;i++){
     if((s[i] == 'x' and s[i+1] == 'y') or (s[i]=='y' and s[i+1] == 'x')){
         ans++;
         i++;
     }
 }
 cout<<ans<<endl;
}

void file_i_o()
{
    ios_base::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);
        #ifndef ONLINE_JUDGE
            freopen("test ip.txt","r", stdin);
            freopen("test op.txt","w", stdout);
        #endif
}
int main() {
    clock_t begin = clock();
    file_i_o();
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    #ifndef ONLINE_JUDGE 
        clock_t end = clock();
        cout<<"\n\nExecuted In: "<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
    #endif 
return 0;
}
1 Like

This will check only for ‘xy’ (not for ‘yx’).

this is wrong, pls check again

Why is it tagged as dp? There is no need. Moreover Dp giving TLE.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static int solve(String s){
        char []arr = s.toCharArray();
        int count = 0;
        int i=0;
        while(i<arr.length-1){
            boolean flag = true;
            if(i+1 < arr.length && arr[i] == 'x' && arr[i+1] == 'y'){
                count++;
                i+= 2;
                flag = false;
            }
            if(i+1 < arr.length && arr[i] == 'y' && arr[i+1] == 'x'){
                count++;
                      flag = false;
                i+= 2;
            }
            if(flag){
            i++;
            }
        }
        return count;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
	    
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0){
		    String s = sc.next();
		    int ans = solve(s);
		    System.out.println(ans);
		}
	}
}