/ 科创班 /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 3ms 256.0 KiB
#2 Accepted 17ms 256.0 KiB
#3 Accepted 4ms 256.0 KiB
#4 Accepted 3ms 256.0 KiB
#5 Accepted 3ms 256.0 KiB
#6 Accepted 2ms 256.0 KiB
#7 Accepted 13ms 256.0 KiB
#8 Accepted 2ms 256.0 KiB
#9 Accepted 7ms 256.0 KiB

代码

#include <stdio.h>

void swap(char* a, char* b)
{
    char c = *a;
    *a = *b;
    *b = c;
}

int find(char*p, char*q)
{
    char*o = q - 1;
    while (o > p && *o != *p) o--;
    return o - p;
}

int rfind(char*p, char* q)
{
    char* o = q + 1;
    while (o < p && *o != *p) o++;
    return p - o;
}

int main()
{
    int i, j, k, len, count[26] = {0};
    char str[8000];
    scanf("%d", &len);
    scanf("%s", str);
    for (i = 0; i < len; i++)
        count[str[i]-'a']++;
    int odd = 0 ;
    for (i = 0; i < 26; i++)
        if (count[i] % 2 == 1)
            odd++;
    if (odd <= 1)
    {
        int change = 0;
        for (i = 0; i < len / 2; i++)
        {
            if (count[str[i]-'a'] == 1)
                break;
            count[str[i]-'a'] -= 2;
            k = find(str + i, str + len - i);
            for (j = i + k; j < len - i - 1; j++)
            {
                swap(str + j, str + j + 1);
                change++;
            }
        }
        if (i != len / 2)
        {
            for (i = len - i - 1; i > len / 2; i--)
            {
                k = rfind(str + i, str + len - i - 1);
                for (j = i - k; j > len - i - 1; j--)
                {
                    swap(str + j, str + j - 1);
                    change++;
                }
            }
        }
        printf("%d", change);
    }
    else
    {
        printf("Impossible");
    }
    return 0;
}

信息

递交者
类型
递交
题目
完美的代价 c
题目数据
下载
语言
C
递交时间
2018-10-20 17:40:10
评测时间
2018-10-20 17:40:10
评测机
分数
100
总耗时
59ms
峰值内存
256.0 KiB