POJ Evil Straw Warts Live (贪心)
来源:互联网 发布:mac电脑打字特别卡 编辑:程序博客网 时间:2024/06/05 16:49
Evil Straw Warts Live
Time Limit: 2000MS
Memory Limit: 65536KTotal Submissions: 850
Accepted: 239
Memory Limit: 65536KTotal Submissions: 850
Accepted: 239
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
题目大意:给一个单词,为了让它变成一个回文串,允许相邻的两个数交换。求最少的交换次数,使得单词变成一个回文串。
解题思路:首先判断一下字母出现的奇数次的个数是否大于1, 大于1不满足。然后从左右开始遍历判断tmp[left] 和 tmp[right]是否相同,相同跳过, 不相同的话找到从左边开始第一个使得tmp[left + l] == tmp[right], 和从右边开始第一个使得tmp[left] == tmp[right + r]。比较l 和 r 的大小然后考虑该移动哪一边。
但是在这里我只用了temp[left]==temp[right-r],将所有的r求和即可。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; char arr[10000]; int b[28]; int solve() { int ans=0,len=strlen(arr),high=len-1; for(int i=0;i<(len+1)/2-1;i++){ if(b[arr[i]-'a']>1){ for(int j=high;j>i;j--){ if(arr[i]==arr[j]){ for(int k=j+1;k<=high;k++){ arr[k-1]=arr[k]; ans++; } arr[high]=arr[i]; high--; break; } } b[arr[i]-'a']-=2; } else{ swap(arr[i],arr[i+1]); ans++; --i; } } return ans; } int main() { int n; scanf("%d",&n); while(n--){ memset(b,0,sizeof(b)); scanf("%s",arr); for(int i=0;arr[i];++i){ ++b[arr[i]-'a']; } int cnt=0; for(int i=0;i<27;i++){ if(b[i]%2) cnt++; } if(cnt>=2){ printf("Impossible\n"); continue; } else{ printf("%d\n",solve()); } } return 0; }
0 0
- POJ 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 (贪心)
- Evil Straw Warts Live (Uva10716 回文串+贪心)
- [贪心]UVA10716 - Evil Straw Warts Live
- UVA - 10716 Evil Straw Warts Live 贪心
- 贪心算法之Evil Straw Warts Live
- Evil Straw Warts Live
- poj 1854 Evil Straw Warts Live
- POJ 1854 Evil Straw Warts Live 笔记
- POJ-1854 Evil Straw Warts Live
- poj 1854 Evil Straw Warts Live(分治)
- 10716 - Evil Straw Warts Live
- UVaOJ10716 - Evil Straw Warts Live
- UVA10716 - Evil Straw Warts Live
- PHP基础排序算法(五)希尔排序
- hdu 5636 Shortest Path(Floyd最短路)
- 164.Examine the command that is used to create a table: SQL> CREATE TABLE orders( oid NUMBER(6) PRI
- 【设计模式】结构型模式之组合Composite
- MySql在windows上的安装
- POJ Evil Straw Warts Live (贪心)
- 第一章简洁代码,二章有意义的命名
- HashMap与Hashtable区别
- mysql启动报错:Another MySQL daemon already running with the same unix socket.
- 40:Spark Streaming中KafkaReceiver内幕实现彻底解密
- Java的内存回收机制
- Java中JDK和JRE的区别是什么?它们的作用分别是什么?
- 一步一步学习JNI(二)
- 接口测试抓包发包工具