Evil Straw Warts Live
来源:互联网 发布:南京弘历软件 编辑:程序博客网 时间:2024/05/21 17:22
Evil Straw Warts Live
总时间限制: 2000ms 内存限制: 65536kB
描述
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 8000 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.
样例输入
3
mamad
asflkj
aabb
样例输出
3
Impossible
2
题意:给定一些字符串,要求出能否通过交换字母变换为回文。。如果可以输出最少变换次数。。
思路:贪心。。
1、先判断能不能变换为回文。。如果字符串中没有或只有1个字母是奇数。就可以组成。。
2、每次从第一个字母开始。从后往前找到一个相同字母。放到最后就是匹配了。。每次移动的次数为当前位置到最后的位置的距离。
要注意有单个字母为奇数的情况。。最后要把这个字母另外拿出来移到最中间。。一开始没考虑这个wa了- -
#include <stdio.h>#include <string.h>//http://bailian.openjudge.cn/exam/3658/5///并不会做,神奇的贪心算法 int t, len, sum, judge, end, vis[8005], mark[8005];char sb[8005], v;void Init() { sum = 0; judge = 1; memset(vis, 0, sizeof(vis)); memset(mark, 0, sizeof(mark)); gets(sb); len = strlen(sb); end = len - 1;}void Judge() {//判断能不能组成回文 int bo = 0; for (int i = 0; i < len; i ++) vis[sb[i] - 'a'] ++; for (int i = 0; i < 26; i ++) { if (vis[i] % 2) { bo ++; if (bo == 2) { judge = 0; break; } v = i + 'a'; } }}void solve() {//变换 for (int i = 0; i < len / 2; i ++) { int j; for (j = end; j >= i + 1; j --) if (sb[j] == sb[i]) { mark[i] = 1; sum += end - j; for (int k = j; k < end; k ++) sb[k] = sb[k + 1]; end --; break; } } if (len % 2) {//奇数情况 for (int i = 0; i < len; i ++) if (sb[i] == v && mark[i] == 0) { sum += len / 2 - i; break; } } if (judge) printf("%d\n", sum); else printf("Impossible\n");}int main() { scanf("%d%*c", &t); while (t --) { Init(); Judge(); solve(); } return 0;}
- Evil Straw Warts Live
- 10716 - Evil Straw Warts Live
- UVaOJ10716 - Evil Straw Warts Live
- UVA10716 - Evil Straw Warts Live
- uva10716 - - 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
- uva 10716 - Evil Straw Warts Live(greedy)
- [贪心]UVA10716 - Evil Straw Warts Live
- UVa 10716 - Evil Straw Warts Live
- UVA - 10716 Evil Straw Warts Live 贪心
- Poj --1751 highways (最小生成树,kruskal算法)
- CentOS 6下快速搭建ftp服务器
- KNN算法demo(R)
- C# 转换TTF为GB2312点阵字库
- 落单的数(落单的数 & 落单的数 II & 落单的数 III)
- Evil Straw Warts Live
- K的倍数----2018美团校招内推
- HDU 4899 Hero meet devil(DP套DP)
- golang 链式操作
- android property机制
- 找不到元素 'jbpm-configuration' 的声明。
- Paxos Made Simple【翻译】
- 缓存一致性和跨服务器查询的数据异构解决方案canal
- JAX-RS 如何传输 JSON 格式的数据 ?