poj 1854 Evil Straw Warts Live

5 篇文章 0 订阅


Evil Straw Warts Live
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 728 Accepted: 198

Description

A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a palindrome. By swap we mean reversing the order of two adjacent symbols. For example, the string "mamad" may be transformed into the palindrome "madam" with 3 swaps: 
swap "ad" to yield "mamda" 
swap "md" to yield "madma" 
swap "ma" to yield "madam" 

Input

The first line of input gives n, the number of test cases. For each test case, one line of input follows, containing a string of up to 8000 lowercase letters.

Output

Output consists of one line per test case. This line will contain the number of swaps, or "Impossible" if it is not possible to transform the input to a palindrome. 

Sample Input

3
mamad
asflkj
aabb

Sample Output

3
Impossible
2



题目的大概意思是让你判断输入的字符串是否能变为回文数,若不能,则输出Impossible,若能,则输出从该字符串变为回文串所需要的最少的步数,(一次只能交换相邻的两个数)。

如何判断该串能否变为回文串:题中已经说明,输入的字符串中只有小写的字母,所以统计各个字母出现的频率,若出现两个或以上的字母频率为奇数,则该字符串,不可能变为回文串。

怎么找出最少的步数:这里用到了贪心的思想。具体看代码。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 8010
int n,ans;
char arr[maxn];
int b[27];
void solve();
int main()
{
    scanf("%d",&n);
    while(n--){
        memset(b,0,sizeof(b));
        scanf("%s",arr);
        int cnt = 0;
        ans = 0;
        for(int i = 0 ; arr[i];i++){
            b[arr[i] - 'a'] ++;
        }
        for(int i = 0; i < 27;i++){
            if(b[i] % 2 == 1){
                cnt ++;
            }
        }
         if(cnt >= 2){
            printf("Impossible\n");
            continue;
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
void solve(){
    int len = strlen(arr);
    int high = len - 1;
    for(int i = 0; i < (len+1)/ 2 - 1;i++){
        if(b[arr[i] - 'a'] % 2 == 0 || b[arr[i] - 'a'] > 1){
            for(int j = high; j > i; j--){
                if(arr[j] == arr[i]){
                    char tmp = arr[j];
                    for(int k = j + 1; k <= high; k++){
                        arr[k-1] = arr[k];
                        ans++;
                    }
                    arr[high] = tmp;
                    high --;
                    break;
                }
            }
            b[arr[i] - 'a'] -= 2;
        }
        else{
            swap(arr[i],arr[i+1]);
            i--;
            ans ++;
        }
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值