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 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"

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 100 lowercase letters. 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

Output for Sample Input

3
Impossible
2

Gordon V. Cormack

Source

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: General Problem Solving Techniques :: Exercises:
Intermediate

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Prominent Problemsetters :: Gordon V. Cormack

Submit Status

给出一个字符串,看这个字符串能否组成回文串,如果能则输出需要交换字母的次数,否则输出Impossible

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;

int T;
int num[30];
char str[105];

int main() {
	scanf("%d", &T);
	while(T--) {
		memset(num, 0, sizeof(num));
		scanf("%s", str);
		int len = strlen(str), cnt = 0;
		for(int i = 0; i < len; i++) num[str[i] - 'a'] ++;
		for(int i = 0; i < 26; i++)
			if(num[i] & 1) cnt++;
		if(cnt >= 2) {
			printf("Impossible\n");
			continue;
		}

		int ans = 0, max = len - 1;
		for(int i = 0; i < len / 2; i++) {
			int j;
			for(j = max; j > i; j--) {
				if(str[j] == str[i]) break;
			}

			if(i == j) {
				swap(str[i + 1], str[i]);
				i--;
				ans++;
				continue;
			}

			ans += max - j;
			for(int k = j + 1; k <= max; k++) str[k - 1] = str[k];
			max--;

		}
		printf("%d\n", ans);
	}
	return 0;
} 
时间: 2024-11-05 02:02:22

UVA - 10716 - Evil Straw Warts Live (简单模拟)的相关文章

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

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

UVA10716 - Evil Straw Warts Live

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

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

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

HDU-1034-Candy Sharing Game(C++ &amp;&amp; 简单模拟)

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3703    Accepted Submission(s): 2311 Problem Description A number of students sit in a circle facing their teacher in the cent

Jquery源码分析与简单模拟实现

前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1.jQuery为什么能使用$的方式调用,$是什么.$()又是什么.链式调用如何实现的 2.jQuery的类级别的扩展内部是怎样实现的,方法级别的扩展有是怎样实现的,$.fn又是什么 3.jQuery选择器是如何执行的,又是如何将结果包装并返回的 带着这些问题,我们进行jquery的模拟实现,文章下方有

Linux 内核 链表 的简单模拟(2)

接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head);

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

JavaWeb学习总结(四十九)——简单模拟Sping MVC

在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: 1 /** 2 * 使用Controller注解标注LoginUI类 3 */ 4 @Controller 5 public class LoginUI { 6 7 //使用RequestMapping注解指明forward1方法的访问路径 8 @RequestMapping("LoginUI/Lo