LAPIN - Editorial

why is this wrong

    #include<iostream>
    #include<string>
   #include<vector>
   using namespace std;

int main()
{
    int t;
    cin>>t;
    vector<string> ans;

    while(t--)
    {
        string str;
        cin>>str;

        while(str.length()>1)
        {
            int len = str.length();
            int last_of_first_half;
            int first_of_second_half;
            int sum1,sum2;
            sum1=sum2=0;

            char ch = str[0];
            if(len%2!=0)
            {
                last_of_first_half = len/2 -1 ;
                first_of_second_half = len/2 +1 ;
            }
            else
            {
                last_of_first_half = len/2 -1 ;
                first_of_second_half = len/2  ;
            }
            string temp="";
           for(int i=0;i<=last_of_first_half;i++)
           {
               if(str[i]==ch)
                sum1++;
               else
               {
                   temp+=str[i];
               }
           }
            for(int j=first_of_second_half;j<len;j++)
            {
               if(str[j]==ch)
                sum2++;
               else
               {
                   temp+=str[j];
               }
            }
            if(sum1!=sum2)
                break;
            else
                str=temp;
        }

        if(str.length()<2)
            ans.push_back("YES");
        else
            ans.push_back("NO");
    }

    for(auto i=ans.begin();i!=ans.end();i++)
        cout<<*i<<"\n";
    return 0;
}

#include
#include
using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++)
{ int half;
int count=0;

    string s;
    cin>>s;
    
    
    int len=s.length();
    if(len%2 == 0)
    {
        half=len/2;
         string s1[half];
    string s2[half];
    for(int j=0;j<half;j++)
    {
        s1[j]=s[j];
        s2[j]=s[len-j-1];
        
    }
    for(int j=0;j<half;j++)
    {for(int k=0;k<half;k++)
    {
        if(s1[j]==s2[k])
        {
            count++;
            s1[j]=j;
            s2[k]=j;
            
            
        }
       
        
    }}

    
      if(count==half)
        {
            cout<<"YES";
            
        }
     
        else
        {cout<<"NO";}
        
       cout<<endl;
    
}
     
    
    else
    {
    
      half=(len+1) /2;
      
    
    string s1[half-1];
    string s2[half-1];
    for(int j=0;j<half-1;j++)
    {
        s1[j]=s[j];
        s2[j]=s[len-j-1];
        
    }
    for(int j=0;j<half-1;j++)
    {for(int k=0;k<half-1;k++)
    {
        if(s1[j]==s2[k])
        {
            count++;
            s1[j]=j;
            s2[k]=j;
            
            
        }
     
        
    }}

    
      if(count==(half-1))
        {
            cout<<"YES";
            
        }
     
        else
        {cout<<"NO";}
        
       cout<<endl;
    
}
}

return 0;

}

i used different approach , no sorting no array declaration, is this best one compared to other ?
https://www.codechef.com/viewsolution/27532994

My code gets compiled and run successfully but it doesnt get accepted during submission
Can you please point out the reason why it is like that from the following code ?
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
String ans="";
while(T>0){
String b=br.readLine();
ans+=solve(b);
T–;
}
System.out.print(ans);
}
static String solve(String x){
int len=x.length();
String com1=null;
String com2=null;
if(len%2==0){
com1=x.substring(0,len/2);
com2=x.substring(len/2,len);
}else{
com1=x.substring(0,len/2);
com2=x.substring((len/2)+1,len);
StringBuilder sb = new StringBuilder(com2);
String com3=sb.reverse().toString();
if(com1.equals(com3)){
return “YES\n”;

  }
   }
   if(com1.contentEquals(com2))
       return "YES\n";
     else
       return "NO\n";
} 

}

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

Thank you for replying !! Here is the link to my submission:-https://www.codechef.com/viewsolution/27638684

1 Like

Thanks - your submission fails for the following testcase:

1
abba
1 Like

How did you check this?
i mean how did you know the reason for rejection of my submission!!

I usually write a testcase generator for the problems I solve - I droned on about the process here. It was then a simple matter to check the output from your program against known-correct answers to random testcases until one failed :slight_smile:

Edit:

To be clear - I don’t know if Codechef run that particular abba testcase, but it’s definitely a testcase that exposes a flaw in your program :slight_smile:

3 Likes

Okay thank you my friend for helping me out and teaching me new stuff. I have successfully removed that flaw from my code and tried to resubmit but still my submission gets rejected and its very frustrating. Here is the link of my new submission https://www.codechef.com/viewsolution/27640147
Please have a look in it.

This fails on

1
abbbabbab

I suggest re-reading the Problem very carefully, as your current approach isn’t going to work :confused:

3 Likes

Okay thank you for helping me !! :smile:

1 Like

thanks once again ssjgz can u tell me how to generate testcases

Here’s lapindrome complete with testcase generator:

// Simon St James (ssjgz) - 2019-10-29
// 
// Solution to: https://www.codechef.com/problems/LAPIN
//
#include <iostream>
#include <array>

#include <cassert>

#include <sys/time.h> // TODO - this is only for random testcase generation.  Remove it when you don't need new random testcases!

using namespace std;

template <typename T>
T read()
{
    T toRead;
    cin >> toRead;
    assert(cin);
    return toRead;
}

constexpr auto numLetters = 26;

array<int, numLetters> letterHistogram(const string& s)
{
    array<int, numLetters> letterHistogram = {};
    for (const auto letter : s)
    {
        letterHistogram[letter - 'a']++;
    }
    return letterHistogram;
}

bool isLapindrome(const string& s)
{
    const int length = s.length();
    const auto leftHalf = s.substr(0, length / 2);
    const auto rightHalf = s.substr(length - length / 2);

    return letterHistogram(leftHalf) == letterHistogram(rightHalf);
}

#if 0
SolutionType solveOptimised()
{
    SolutionType result;
    
    return result;
}
#endif


int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    if (argc == 2 && string(argv[1]) == "--test")
    {
        struct timeval time;
        gettimeofday(&time,NULL);
        srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
        //const int T = rand() % 100 + 1;
        const int T = 1;
        cout << T << endl;

        for (int t = 0; t < T; t++)
        {
            const int len = 1 + rand() % 12;
            const int maxChar = 1 + rand() % numLetters;

            string s;
            for (int i = 0; i < len; i++)
            {
                s.push_back('a' + rand() % maxChar);
            }
            cout << s << endl;
        }

        return 0;
    }
    
    const auto T = read<int>();

    for (int t = 0; t < T; t++)
    {

        const auto s = read<string>();
        cout << (isLapindrome(s) ? "YES" : "NO") << endl;
    }

    assert(cin);
}

To generate a testcase, just run the executable and pass it the --test flag on the command-line e.g if the executable is called a.out:

./a.out --test

See also:

1 Like

The implementation of the logic as given in Editorial in Python3

t = int(input())
while(t>0):
    d =  {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
    s = input()
    ln = len(s)
    fg = 0
    s1 = ""
    s2 = ""
    if(ln % 2 == 0):
        s1 = s[:ln//2]
        s2 = s[ln//2:]
        # print(s1, s2)
    else:
        s1 = s[:ln//2]
        s2 = s[(ln//2)+1:]
        # print(s1, s2)
    
    for i in s1:
        d[i] += 1
    
    for j in s2:
        d[j] -= 1
        
    # print(d)
    
    for i in d.values():
        if(i != 0):
            fg = 1
            break
    if(fg == 1):
        print("NO")
    else:
        print("YES")
    t -= 1

Can somebody help me out whats wrong in my code?
https://www.codechef.com/viewsolution/30273757

Consider the testcase:

5
bbcccbe
gedce
eccbdde
bdfbdbe
egafded
3 Likes

tnx bro…i got it…i didn’t clear the map.

1 Like

Even better would be to just write your code so that this kind of problem simply can’t happen in the first place; none of l,l1,i,s or m are used outside of the while block, so why declare them there?

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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int l,l1,i;
        string s;
        map<char,int> m;
        cin>>s;
        l=s.size()/2;
        if(l==0)
        {
            cout<<"NO\n";
            continue;
        }
        if(s.size()%2==0)
        {

            l1=l;
        }
        else
        {
            l1=l+1;
        }
        i=l1;
        while(i<s.size())
        {
            if(m.find(s[i])==m.end())
            {
                m.insert(pair<char,int>(s[i],1));
            }
            else
                m[s[i]]++;
            i++;
        }
        for(i=0;i<l;i++)
        {
            if(m.find(s[i])==m.end()|| m[s[i]]==0)
            {
                cout<<"NO\n";
                break;
            }
            else
                m[s[i]]--;
        }
        if(i==l)
        {
            cout<<"YES\n";
        }
    }
    return 0;
}

or better still:

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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        const int l=s.size()/2;
        if(l==0)
        {
            cout<<"NO\n";
            continue;
        }
        int l1 = l;
        if(s.size()%2!=0)
        {
            l1=l + 1;
        }
        int i=l1;
        map<char,int> m;
        while(i<s.size())
        {
            if(m.find(s[i])==m.end())
            {
                m.insert(pair<char,int>(s[i],1));
            }
            else
                m[s[i]]++;
            i++;
        }
        for(i=0;i<l;i++)
        {
            if(m.find(s[i])==m.end()|| m[s[i]]==0)
            {
                cout<<"NO\n";
                break;
            }
            else
                m[s[i]]--;
        }
        if(i==l)
        {
            cout<<"YES\n";
        }
    }
    return 0;
}
4 Likes