本文介绍了如何查找字符串是否是Lapindrome?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

以下代码可提供正确的输出,如codechef问题页面上所提供: http://www.codechef.com/problems/LAPIN 但是提交时得到错误的答案请告诉我我的代码可能存在的问题

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

这是问题

羽绒被定义为一个字符串,当在中间拆分时,会给出两个具有相同字符和每个字符相同频率的两半.如果字符串中的字符数为奇数,我们将忽略中间字符并检查lapindrome.例如,gaga是一个lapindrome,因为两个半部ga和ga具有相同的字符且具有相同的频率.另外,abccab,rotor和xyzxy都是lapindromes的一些示例.请注意,abbaab不是lapindrome.这两个半部分包含相同的字符,但它们的频率不匹配.您的任务很简单.给定一个字符串,您需要确定它是否是一个lapindrome.

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.

输入:

输入的第一行包含一个整数T,即测试用例的数量.

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

每个测试都是一行,其中包含仅由小写英文字母组成的字符串S.

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

输出:

对于每个测试用例,在单独的行上输出:如果字符串是lapindrome,则输出"YES";否则,则输出"NO".

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

这是代码

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

推荐答案

我建议您阅读有关模块化的内容;它将使您的生活更轻松.

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

这篇关于如何查找字符串是否是Lapindrome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 15:37