uva 10716 - Evil Straw Warts Live
来源:互联网 发布:家用电脑远程监控软件 编辑:程序博客网 时间:2024/05/16 08:50
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
3mamadasflkjaabb
Output for Sample Input
3Impossible2
描述:
判断一个字符串是否可以经过字符交换转化为回文字符串。
方法:
先统计字符串中各个字符的出现的次数和个数是奇数的字符个数。找出字符串的长度L。如果奇数字符大于1个,返回-1.
从左往右遍历字符串(到长度的一半L/2),对每一个字符
如果其个数大于1,则从字对称位置向头遍历,直到找到第一个相同字符,然后把它移动到对称位置。并记录移动次数,相应字符个数减少2。
如果其个数等于1,与下一个字符交换位置,并重新处理该位置的新字符。记录移动次数(+1)。
代码:
0 0
- UVa 10716 - Evil Straw Warts Live
- UVa 10716 - Evil Straw Warts Live
- uva 10716 - Evil Straw Warts Live
- UVA 10716 - Evil Straw Warts Live
- uva 10716 - Evil Straw Warts Live
- uva 10716 Evil Straw Warts Live
- uva 10716 Evil Straw Warts Live
- UVa:10716 Evil Straw Warts Live
- uva 10716 - Evil Straw Warts Live
- uva 10716 - Evil Straw Warts Live(greedy)
- UVa 10716 - Evil Straw Warts Live
- UVA - 10716 Evil Straw Warts Live 贪心
- uva 10716 Evil Straw Warts Live
- uva 10716 Evil Straw Warts Live
- UVA 10716 Evil Straw Warts Live
- uva 10716 Evil Straw Warts Live
- 10716 - Evil Straw Warts Live
- UVA 10716 Evil Straw Warts Live(贪心)
- tq2440_External Interrupt Controller
- uva 10729 - Treequivalence(暴力+离散)
- @注解_单向一对多映射
- 在线编程挑战赛第一名:我是这么学算法的
- hadoop生态系统
- uva 10716 - Evil Straw Warts Live
- android Notification 的使用
- Windows下SVN命令行工具使用详解(附加TortoiseSVN)
- 匆匆
- @注解_subselect
- 2012蓝桥杯【初赛试题】身份证
- 频编解码之speex-speex编译静态库for iOS 此博文包含图片(2012-08-08 09:43:09)
- C/C++开发者必不可少的15款编译器+IDE
- 第十四章 Spring Bean自动注入(Spring Framework3.1教程)