UVA - 10716 - Evil Straw Warts Live (简单模拟)
2015-04-02 23:55
274 查看
UVA - 10716 Evil Straw Warts Live
Description Problem D: Evil Straw Warts LiveA 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 oftwo 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 Input3 mamad asflkj aabb Output for Sample Input3 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 |
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; }
相关文章推荐
- UVa 602 简单模拟
- Uva 101 - The Blocks Problem//简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVALive - 7000 Present Problem 简单模拟
- Working Hours UVALive - 7017(简单模拟)
- UVA_10716 - Evil Straw Warts Live-------水题(模拟)
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟
- UVA-10716 Evil Straw Warts Live(贪心+模拟)
- UVA 10161-Ant on a Chessboard 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVA 1586 分子量(简单模拟计算)
- 简单模拟 UVa 10267 - Graphical Editor
- uva - 10716 - Evil Straw Warts Live(贪心、模拟)
- UVA 1590 IP Networks 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- (简单模拟2.3.1)UVA 11000 Bee(构造法模拟)