How to find whether the string is a Lapindrome? [closed]

梦想的初衷 提交于 2021-02-05 12:29:51

问题


The following code is giving correct output as given on the codechef problem page: http://www.codechef.com/problems/LAPIN but getting wrong answer on submission please tell me the possible problem with my code

here is the question

Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match. Your task is simple. Given a string, you need to tell if it is a lapindrome.

Input:

First line of input contains a single integer T, the number of test cases.

Each test is a single line containing a string S composed of only lowercase English alphabet.

Output:

For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.

and here is the code

#include<stdio.h>
#include<string.h>
int main()
{
    int f,t,mid,len;
    char arr[1000];
    int left[125],right[125];
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        f=0;
        scanf("%s",arr);
        memset(left,0,sizeof(left));
        memset(right,0,sizeof(right));
        len=strlen(arr);
        for(int i=0;i<len/2;i++)
            left[arr[i]]++;
        for(int i=(len+1)/2;i<len;i++)
            right[arr[i]]++;
        for(int i=0;i<strlen(arr);i++)
        {
            if(left[arr[i]]!=right[arr[i]])
                f++;
            break;
        }
        if(f==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

回答1:


I recommend you read up on modularity; it'll make your life easier.

#include <stdio.h>

#define BOOL unsigned char
#define TRUE 1
#define FALSE 0

unsigned string_length(char *string)
{
    unsigned counter = 0;

    while (string[counter++] != '\0') { }

    return counter - 1;
}

BOOL are_equal(unsigned *a, unsigned *b, int size)
{
    int i;
    for (i = 0; i < size; ++i)
    {
        if (a[i] != b[i])
        {
            return FALSE;
        }
    }

    return TRUE;
}

BOOL is_lapindrome(char *string)
{
    unsigned left[26] = { 0 }, right[26] = { 0 },
             str_len = string_length(string);

    if (str_len < 2)
    {
        return FALSE;
    }

    int i;
    for (i = 0; i <= str_len / 2 - 1; ++i)
    {
        left[string[i] - 'a']++;
    }

    for (i = (str_len + 1) / 2; i < str_len; ++i)
    {
        right[string[i] - 'a']++;
    }

    return are_equal(left, right, 26);
}

int main()
{
    char *list[6] =
    {
        "gaga",
        "abcde",
        "rotor",
        "xyzxy",
        "abbaab",
        "ababc"
    };

    int i;
    for (i = 0; i < 6; ++i)
    {
        printf("%s\n", is_lapindrome(list[i]) == TRUE ? "YES" : "NO");
    }

    return 0;
}



回答2:


  1. Your buffer is one byte too short - a string of 1000 characters requires 1001 chars, the last one taken by the nul terminator.

  2. "lowercase English alphabet" sounds a bit ambiguous - I'd say that by some interpretation, it could contain spaces. If so, the input will be read incorrectly.

I can't see other problems right now, but I'd strongly suspect the first one.



来源:https://stackoverflow.com/questions/21145620/how-to-find-whether-the-string-is-a-lapindrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!