简体   繁体   English

在C中创建一个字符串回文

[英]Make a string Palindrome in C

James found a love letter his friend Harry has written for his girlfriend. 詹姆斯找到了他的朋友哈里为女友写的一封情书。 James is a prankster, so he decides to meddle with the letter. 詹姆斯是个恶作剧者,所以他决定插手这封信。 He changes all the words in the letter into palindromes. 他把信中的所有单词都变成了回文。

To do this, he follows 2 rules: 为此,他遵循2条规则:

(a) He can reduce the value of a letter, eg he can change 'd' to 'c', but he cannot change 'c' to 'd'. (a)他可以降低字母的价值,例如,可以将'd'更改为'c',但不能将'c'更改为'd'。 (b) In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes 'a'. (b)为了形成回文,如果他必须反复降低字母的价值,他可以这样做,直到字母变成'a'。 Once a letter has been changed to 'a', it can no longer be changed. 字母更改为“ a”后,就无法再更改。

Each reduction in the value of any letter is counted as a single operation. 任何字母值的每次减少都视为一次操作。 Find the minimum number of operations required to convert a given string into a palindrome. 找到将给定字符串转换为回文式所需的最少操作数。

Input Format The first line contains an integer T, ie, the number of test cases. 输入格式第一行包含一个整数T,即测试用例的数量。 The next T lines will contain a string each. 接下来的T行将各包含一个字符串。 The strings do not contain any spaces. 字符串不包含任何空格。

Output Format A single line containing the number of minimum operations corresponding to each test case. 输出格式包含与每个测试用例对应的最小操作数的一行。

Constraints 1 ≤ T ≤ 10 1 ≤ length of string ≤ 104 All characters are lower case English letters. 约束1≤T≤10 1≤字符串长度≤104所有字符均为小写英文字母。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int count;
    int result;
    int i,j;

    scanf("%d",&count);

    for (i = 0 ; i < count ; i++){

        result = 0;

        char * string;
        scanf("%ms",&string);

        int k = (int)strlen(string);

        printf("Length: %d\n",k);

        int l = k/2;

        printf("L is: %d\n",l);

        for ( j = 0 ; j < l ; j++){

            printf("first char is is: %c\n",string[j]);
            printf("Second char is is: %c\n",string[k-j-1]);
            printf("Current loop count: %d\n",j);

            if ( string[j] != string [k-j] ){

                int g = (int)(string[j] - string[k-j-1]);
                if ( g > 0){
                    result += g;
                }
                else{
                    result -= g;
                }

            }
            else;

        }

        printf("%d\n",result);

    }
    return 0;
}

Sample Input #00 样本输入#00

4
abc
abcba
abcd
cba

Sample Output #00 样品输出#00

2
0
4
2

Explanation 说明

For the first test case, abc -> abb -> aba. 对于第一个测试用例,abc-> abb-> aba。 For the second test case, abcba is already palindromic string. 对于第二个测试用例,abcba已经是回文字符串。 For the third test case, abcd -> abcc -> abcb -> abca = abca -> abba. 对于第三个测试用例,abcd-> abcc-> abcb-> abca = abca-> abba。 For the fourth test case, cba -> bba -> aba. 对于第四个测试用例,cba-> bba-> aba。

The above stated code is working for the given sample input, but it is not the correct one according to hacckerrank. 上述代码适用于给定的样本输入,但根据hacckerrank来看,它不是正确的代码。 Can someone point out the error please? 有人可以指出错误吗?

The error is here: 错误在这里:

if ( string[j] != string [k-j] ){

The second character should be string[kj-1] . 第二个字符应该是string[kj-1] For instance, when j = 0 , you should compare with string[k-1] to compare the first and last characters of the string. 例如,当j = 0 ,应与string[k-1]比较以比较字符串的第一个和最后一个字符。

Also, you have a memory leak. 另外,您还有内存泄漏。 At the bottom of the outer loop you should do: 在外循环的底部,您应该执行以下操作:

free(string);

I have already solved this question and the correct code is given below. 我已经解决了这个问题,下面给出了正确的代码。 The complexity of code is O(n*t) where n is length of string and t is number of test cases 代码的复杂度为O(n * t),其中n是字符串的长度,t是测试用例的数量

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {   
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int count=0;
        int len=s.length();
        for(int i=0;i<len/2;i++){
            count+=abs(s[i]-s[len-i-1]);
        }
        cout<<count<<"\n";
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM