UVA10716 - Evil Straw Warts Live

题意:如果可以的话,使用最少的交换次数,使得字符串变成回文字符串。

思路:

1、首先我们可以先判断这个字符串是否有成为回文的可能性。当一个字符串中出现两个或两个以上的奇数个数的字符,那么这个字符串一定不能成为回文字符串。

2、之后就要讨论怎么使用最少的交换次数使得变成回文字符串。我们可以采取由外到内的方法,即先将头尾两端的字符交换成相同的,然后left++,right--,慢慢向内靠拢。

为了让交换次数最少,那么每次移动到左右两个端点的字符的交换次数也要最少。这样的话就要找相同字符第一次出现和最后一次出现,它们的距离左右两个端点之和最小的那个字符,然后分别将其第一次出现交换至左端点,最后一次出现交换至右端点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1005;

char str[MAXN];
int vis[MAXN], num[MAXN];
int len;

int judge() {
    memset(num, 0, sizeof(num));
    for (int i = 0; i < len; i++) {
        num[str[i] - 'a']++;
    }
    int cnt = 0;
    for (int i = 0; i < 27; i++)
        if (num[i] % 2)
            cnt++;

    if (cnt > 1)
        return 0;
    return 1;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%s", str);
        len = strlen(str);
        if (!judge())
            printf("Impossible\n");
        else {
            int left = 0, right = len - 1, cnt = 0;
            while (left < right) {
                int lcur = left, rcur = right, temp, Min = INF;
                memset(vis, 0, sizeof(vis));
                for (int i = left; i < right; i++)
                    if (!vis[i]) {
                        vis[i] = 1;
                        int lastcur = i;
                        for (int j = i + 1; j <= right; j++)
                            if (str[i] == str[j]) {
                                vis[j] = 1;
                                lastcur = j;
                            }

                        int temp = i - left + right - lastcur;
                        if (temp < Min) {
                            Min = temp;
                            lcur = i;
                            rcur = lastcur;
                        }
                    }

                for (int i = lcur; i > left; i--) {
                    swap(str[i], str[i - 1]);
                    cnt++;
                }
                for (int i = rcur; i < right; i++) {
                    swap(str[i], str[i + 1]);
                    cnt++;
                }
                left++;
                right--;
            }

            printf("%d\n", cnt);
        }
    }
    return 0;
}

UVA10716 - Evil Straw Warts Live,布布扣,bubuko.com

时间: 2024-08-02 23:52:04

UVA10716 - Evil Straw Warts Live的相关文章

UVA - 10716 - Evil Straw Warts Live (简单模拟)

UVA - 10716 Evil Straw Warts Live Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an

Evil Straw Warts Live (Uva10716 回文串+贪心)

[原题] 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

uva 10716 Evil Straw Warts Live(贪心回文串)

这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看两端位置上的字母是否相等.若不相等就在里面查找能使他们相等且所需移动位置最少的那 个.然后交换.记录交换的距离,贪心的离最后一个由近及远找与第一个位置相等的.同理贪心从第一个位置找和最 后一个位置相等且离第一个位置近期的. . .感觉这样的方法确实能够,可是并不会证明这样的策略的正确性.. . 代码

uva10716Evil Straw Warts Live(贪心)

题目:uva10716Evil Straw Warts Live(贪心) 题目大意:给出一个字符串,问如何交换字母位置能够得到回文.这里求最少的交换次数.如果不能通过交换得到回文,输出Impossible. 交换只允许和相邻的字母进行交换. 解题思路:贪心策略:每次都是先将距离两边距离和最短的对称的字母移到到两边,这样这两个字母就对称了,且交换次数是最少的.然后就将这两个字母从字符串中移除.再用相同的方法接着判断剩下的字符串.直到剩余的字符串长度 <= 1就可以停止了. 代码: #include

uva--10716Evil Straw Warts Live +回文串+贪心

题意: 输入一个字符串,我们可以交换这个字符串中的相邻字符:问至少经过多少步交换可以得到一个回文串:如果无论怎么交换都得不到回文串,输出"Impossible": 思路: 首先由回文串的定义和性质,可以得到两种不可能情况:1.当这个串长度为奇数时,如果出现次数为奇数次字母的数目不为1,则显然不可能.2.当这个串长度为偶数时,如果出现次数为奇数次字母的个数大于0,则不可能. 除去这两种不可能的情况后,这个串就一定可以转成回文串.我们只需要考虑0--len/2(len为串长)的部分,对于第

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

CF 337D Book of Evil 树形DP 好题

Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some p

LightOJ 1029 Civil and Evil Engineer最小生成树和最大生成树

F - Civil and Evil Engineer Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1029 Description A Civil Engineer is given a task to connect n houses with the main electric power station directly

Gym 100463D Evil DFS

Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description Richard is evil. He wants to give another geometry problem to the participants of this contest and I’m afraid I have no choice but to comply. Wh