top1编程
← 返回题目
题解

【提高】完美的代价

1 条题解

  • 0
    @ 2026-7-29 0:17:45
    #include <iostream>  
    #include <cstdio>  
    #include <algorithm>  
    #include <cstring>  
    #include <cctype>  
      
    using namespace std;  
      
    int main()  
    {  
        //freopen("in.txt", "r", stdin);  
        char ch[8010];  
        int n, sum = 0, ok = 1, c = -1;  
        scanf("%d%s", &n, ch);  
        int j = n - 1;  
        for(int i = 0; i <= j-1; i++){        //从左向右依次判断  
            for(int k = j; k >= i; k--){      //从最右边查找,看有无与当前字符相同的  
                if(k == i){     //没有找到与ch[i]相同的字符  
                    if(n % 2 == 0 || c != -1){      //若n为偶数或ch[i]不是唯一无法匹配的字符  
                        ok = 0;  
                        break;  
                    }  
                    c = 1;              //n为奇数,ch[i]为第一个无法匹配的字符  
                    sum += n / 2 - i;       //将它移到中间所需步数  
                    break;  
                }  
                if(ch[k] == ch[i]){         //找到相同的  
                    for(int t = k; t <= j - 1; t++) ch[t] = ch[t + 1];        //往后移到对称位置  
                    sum += j - k;  
                    j--;  
                    break;  
                }  
            }  
            if(!ok) break;  
        }  
        if(!ok) printf("Impossible&#92;n");  
        else printf("%d&#92;n", sum);  
        return 0;  
    }
    
    • 1