uva10716Evil Straw Warts Live(贪心)

题目:uva10716Evil Straw Warts Live(贪心)

题目大意:给出一个字符串,问如何交换字母位置能够得到回文。这里求最少的交换次数。如果不能通过交换得到回文,输出Impossible。

交换只允许和相邻的字母进行交换。

解题思路:贪心策略:每次都是先将距离两边距离和最短的对称的字母移到到两边,这样这两个字母就对称了,且交换次数是最少的。然后就将这两个字母从字符串中移除。再用相同的方法接着判断剩下的字符串。直到剩余的字符串长度 <= 1就可以停止了。

代码:

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

const int N = 105;
const int M = 26;
char s[N];
int visit[N];

int solve () {

	memset (visit, 0, sizeof (visit));
	int len = strlen(s);
	for (int i = 0; i < len; i++)
		visit[s[i] - 'a']++;

	int count = 0;
	for (int i = 0; i < M; i++)
		if (visit[i] % 2)
			count++;

	if ( (len % 2 && count == 1) || (len % 2 == 0 && !count)) {

		int l, r, min;
		count = 0;
		while (len > 1) {

			min = N;
			memset (visit, 0, sizeof (visit));
			for (int i = 0; i < len; i++) {

				if (visit[i])
					continue;
				visit[i] = 1;
				for (int j = len - 1; j >= 0; j--) {

					if (s[j] == s[i] && !visit[j]) {

						visit[j] = 1;
						if (len - abs (i - j) - 1  < min) {

							min = len - abs (i - j) - 1;
							l = i;
							r = j;
						}
						break;
					}
				}

//			printf ("%d\n", min);
			}
			count += min;
//			printf ("%d\n", min);
//			printf ("%d\n", count);
//			printf ("%d %d\n", l, r);

			if (l > r) {

				min = l;
				l = r;
				r = min;
			}

			for (int i = l; i < len - 1; i++)
				s[i] = s[i + 1];
			for (int i = r - 1; i < len - 2; i++)
				s[i] = s[i + 1];
//			s[len - 2] = '\0';
//				printf ("%s\n", s);
			len -= 2;
		}
		return count;
	}
	return -1;
}

int main () {

	int t;
	int count;
	scanf ("%d", &t);
	while (t--) {

		scanf ("%s", s);

		count = solve();
		if (count < 0)
			printf ("Impossible\n");
		else
			printf ("%d\n", count);
	}
	return 0;
}

uva10716Evil Straw Warts Live(贪心)

时间: 2024-08-26 08:57:00

uva10716Evil Straw Warts Live(贪心)的相关文章

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

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

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

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

UVA10716 - Evil Straw Warts Live

题意:如果可以的话,使用最少的交换次数,使得字符串变成回文字符串. 思路: 1.首先我们可以先判断这个字符串是否有成为回文的可能性.当一个字符串中出现两个或两个以上的奇数个数的字符,那么这个字符串一定不能成为回文字符串. 2.之后就要讨论怎么使用最少的交换次数使得变成回文字符串.我们可以采取由外到内的方法,即先将头尾两端的字符交换成相同的,然后left++,right--,慢慢向内靠拢. 为了让交换次数最少,那么每次移动到左右两个端点的字符的交换次数也要最少.这样的话就要找相同字符第一次出现和最

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题目分类

题目 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

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro