回文串问题

无意中看到一个网友提的问题:关于c语言回文串的问题,一时无聊,随手写了一个

问题描述

  回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的。小龙龙认为回文串才是完美的。现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串。
  交换的定义是:交换两个相邻的字符
  例如mamad
  第一次交换 ad : mamda
  第二次交换 md : madma
  第三次交换 ma : madam (回文!完美!)
输入格式
  第一行是一个整数N,表示接下来的字符串的长度(N <= 8000)
  第二行是一个字符串,长度为N.只包含小写字母
输出格式
  如果可能,输出最少的交换次数。
  否则输出Impossible
样例输入
5
mamad
样例输出
3

据说这个是蓝桥杯的题目,一听就是学生时代的竞赛。作为一个中老年码农,上学那会儿电脑没有那么普及,也没有这样的比赛。专门去搜了一下什么是蓝桥杯,呵呵,好像很专业的样子啊。赞助商里面,做外包的居多啊,青年朋友们擦亮眼睛啊。

思路

只能两辆互换,这个有点麻烦。两头一起向中间凑。下面是两个栗子。

字符串交换序号
ma m ad0
maa m d1
maad m2
mad a m3

字符串交换序号
m m aad0
ma m ad1
maa m d2
maad m3
mad a m4

参考代码

以下实现的空间复杂度为O(n),时间复杂度为O(n^2)。

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

#define MAX_INPUT_LEN 8000

int main()
{
    int alpha[26]={0};  // statistical bucket
    int nLen = 0;       // length of the input string
    int nScanLen = 0;
    int swap_num = 0;   // count of swap
    int odd_char = 0;   // number of chars with odd appearance
    int even_char = 0;  // number of chars with even appearance
    char str[MAX_INPUT_LEN]={0};
    char* pcur = str;

    int i,j,k;

    scanf("%d", &nLen);
    scanf("%s", str);
    nScanLen = strlen(str);
    if (nScanLen != nLen) return 0;
    if (nLen == 0) return 0;

    i=0;
    do{
        alpha[str[i++] -'a']++;
    }while(str[i]!=0);

#ifdef _DEBUG
    printf("\n");
#endif
    for(i=0; i<26;i++)
    {
#ifdef _DEBUG
        if(alpha[i]!=0)printf("DBG: [%c] = %d\n",i+'a', alpha[i]);
#endif
        odd_char += alpha[i]%2;
        even_char += alpha[i] && !(alpha[i]%2);
    }

#ifdef _DEBUG
    printf("DBG: odd_char = %d, even_char = %d\n",odd_char, even_char);
#endif
    if( odd_char == (nLen%2 ? 1 : 0))
    {
#ifdef _DEBUG
        printf("DBG: swap[%4d], str = %s\n", swap_num, str);
#endif
        i = 0;
        j = nLen-1-i;
        // from both ends to the middle
        while(i<=j)
        {
#ifdef _DEBUG
            printf("DBG: i=%d, j=%d\n",i,j);
#endif
            k = j;
            // from right to left , find the first position equal to the left current char
            while(k>i)
            {
                if (str[k]!=str[i]) k--;
                else                break;
            }
            // swap the found char to its target position
            while(k<j)
            {
                str[k] ^= str[k+1];
                str[k+1] ^= str[k];
                str[k] ^= str[k+1];
                k++;
                swap_num++;
#ifdef _DEBUG
                printf("DBG: swap[%4d], str = %s\n", swap_num, str);
#endif
            }
            i++;
            j = nLen-1-i;
        }
#ifdef _DEBUG
        printf("\nswap = ");
#endif
        printf("%d\n",swap_num);
    }
    else
    {
        printf("\nImpossible\n");
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值