poj 1854 Evil Straw Warts Live
来源:互联网 发布:linux打包命令 tar 编辑:程序博客网 时间:2024/06/01 22:11
Evil Straw Warts Live
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 728 Accepted: 198
Description
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"
swap "ad" to yield "mamda"
swap "md" to yield "madma"
swap "ma" to yield "madam"
Input
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
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
Sample Output
3Impossible2
题目的大概意思是让你判断输入的字符串是否能变为回文数,若不能,则输出Impossible,若能,则输出从该字符串变为回文串所需要的最少的步数,(一次只能交换相邻的两个数)。
如何判断该串能否变为回文串:题中已经说明,输入的字符串中只有小写的字母,所以统计各个字母出现的频率,若出现两个或以上的字母频率为奇数,则该字符串,不可能变为回文串。
怎么找出最少的步数:这里用到了贪心的思想。具体看代码。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 8010int n,ans;char arr[maxn];int b[27];void solve();int main(){ scanf("%d",&n); while(n--){ memset(b,0,sizeof(b)); scanf("%s",arr); int cnt = 0; ans = 0; for(int i = 0 ; arr[i];i++){ b[arr[i] - 'a'] ++; } for(int i = 0; i < 27;i++){ if(b[i] % 2 == 1){ cnt ++; } } if(cnt >= 2){ printf("Impossible\n"); continue; } solve(); printf("%d\n",ans); } return 0;}void solve(){ int len = strlen(arr); int high = len - 1; for(int i = 0; i < (len+1)/ 2 - 1;i++){ if(b[arr[i] - 'a'] % 2 == 0 || b[arr[i] - 'a'] > 1){ for(int j = high; j > i; j--){ if(arr[j] == arr[i]){ char tmp = arr[j]; for(int k = j + 1; k <= high; k++){ arr[k-1] = arr[k]; ans++; } arr[high] = tmp; high --; break; } } b[arr[i] - 'a'] -= 2; } else{ swap(arr[i],arr[i+1]); i--; ans ++; } }}
0 0
- poj 1854 Evil Straw Warts Live
- POJ 1854 Evil Straw Warts Live 笔记
- POJ-1854 Evil Straw Warts Live
- poj 1854 Evil Straw Warts Live(分治)
- Evil Straw Warts Live
- POJ 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
- [leetcode 47] Permutations II
- VMware网络连接相关
- MAC下的反编译、反汇编和调试神器Hopper Disassembler
- Windows下搭建PHP开发环境
- 表单元素的placeholder属性
- poj 1854 Evil Straw Warts Live
- slab着色
- oracle - 在查询中使用Decode,Case,NVL进行逻辑判断
- linux group
- centos 6.5 卸载OpenJDK, 安装JDK
- PHP操作SQL Server 2008/2012
- [leetcode 93] Restore IP Addresses
- 【HDU 3949】XOR
- 设计模式-模版方法模式