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" 

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

3
mamad
asflkj
aabb

Sample Output

3
Impossible
2

Source

Waterloo local 2004.09.19

输入一串字符,先判断是否可以通过变换顺序变成回文串,不是的话输出impossible。

然后如果possible,定义一次swap相邻两个字母为一步,计算这个字符串经过最少多少次swap可以变为回文串。

具体思维:使用分治的方法……

每次搞定最左边和最右边的两个字母,也就是从外向内一层层做成回文串。

比如 abcbac 这个,先看最左边的“a”,从最右边开始遍历字符串,找到的第一个“a”就可以经过最少次数把右边变成“a”,

再看最右边的“c”,同样的,从最左边遍历字符串,找到的第一个“c”就可以经过最少次数把右边变成“c”,

比较一下是把最外层变成两个a还是两个c……哪个划算,就加上这个步数,把字符串最外层排好:“abcbca”,then去掉最外层变成:“bcbc”,继续重复上面的工作……

 #include<cstring>
#include<string>
using namespace std;
void swap_palindrome(char s[],int begin,int end,int &step)
{
if(end-begin<=) return;
int i,left_distance,right_distance;
for(i=begin;i<end;i++) if(s[i]==s[end]) break;
left_distance=i-begin;
for(i=end;i>begin;i--) if(s[i]==s[begin]) break;
right_distance=end-i;
if(left_distance<right_distance){
step+=left_distance;
for(int i=left_distance+begin;i>begin;i--) swap(s[i],s[i-]);
}else{
step+=right_distance;
for(int i=end-right_distance;i<end;i++) swap(s[i],s[i+]);
}
swap_palindrome(s,begin+,end-,step);
}
int is_palindrome(char s[])
{
int letter[]={},len=strlen(s);
for(int i=;i<len;i++) letter[ (s[i]-'a') ]++;
int count=;
for(int i=;i<;i++){
if(letter[i]%==) count++;
}
if(count>) return ;
else return ;
}
int main()
{
int n;scanf("%d",&n);
char s[];
while(n--){
scanf("%s",s);
if(!is_palindrome(s)) printf("Impossible\n");
else{
int begin=,end=strlen(s)-,step=;
swap_palindrome(s,begin,end,step);
printf("%d\n",step);
}
}
return ;
}

另外……这题,刚开始我用了string类型,cin输入,迭代器遍历……就time limit exceeded……

POJ 1854 - Evil Straw Warts Live的更多相关文章

  1. poj 1854 Evil Straw Warts Live 变成回文要几次

    Evil Straw Warts Live Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1799   Accepted: ...

  2. UVA 10716 Evil Straw Warts Live(贪心)

    Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when re ...

  3. UVa 10716 - Evil Straw Warts Live

    题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...

  4. uva 10716 Evil Straw Warts Live(贪心回文串)

    这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...

  5. POJ 1854 贪心(分治)

    Evil Straw Warts Live Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1144   Accepted:  ...

  6. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  9. 【POJ】3207 Ikki's Story IV - Panda's Trick

    http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...

随机推荐

  1. iOS 自动移除KVO观察者

    对NSObject写一个分类: #import <Foundation/Foundation.h> @interface NSObject (FMObserverHelper) - (vo ...

  2. 【C#】C#中方法(函数)的类型有哪些

    目录结构: contents structure [+] 构造函数 引用类型的构造函数 值类型的构造函数 析构函数 析构函数的使用 析构函数和Dispose()方法的区别 操作符重载 转化操作符方法 ...

  3. Atitit  验证 数字验证 非空验证的最佳算法  h5

    Atitit  验证 数字验证 非空验证的最佳算法  h5 <td><select class="searchBox-select"   style=" ...

  4. What-is-DevOps

    https://www.quora.com/What-is-DevOps https://neoteric.eu/blog/devops-a-culture-of-getting-things-don ...

  5. ArcGIS Runtime SDK for iOS开发地图图层-图形图层

    注:本文翻译自:https://developers.arcgis.com/ios/objective-c/guide/creating-a-graphics-layer.htm        创建图 ...

  6. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

    文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...

  7. Android美丽的对话框项目sweet-alert-dialog

    美丽的对话框 sweet-alert-dialog 项目地址: https://github.com/pedant/sweet-alert-dialog android原生的dialog太生硬了,之前 ...

  8. Java知多少(22)方法重载

    在Java中,同一个类中的多个方法可以有相同的名字,只要它们的参数列表不同就可以,这被称为方法重载(method overloading). 参数列表又叫参数签名,包括参数的类型.参数的个数和参数的顺 ...

  9. 记录一次在安装双系统的过程(先有debian, 后加windows 8.1)

    突破的难题:  如何在一整个根目录的分区中切出来一半用于安装windows8.1呢? 当初安装debian系统时, 由于脑残,直接把整个500G的硬盘分给了根目录 /  ,所以整个磁盘就TMD的一个主 ...

  10. centos7系统下安装php-fpm并配置nginx支持并开启网站gzip压缩

    注:此处不介绍nginx的安装.以下教程默认已安装nginx. 1. yum install -y php-fpm yum install php-pdo yum install php-mysql ...