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

  1. 3
  2. mamad
  3. asflkj
  4. aabb

Sample Output

  1. 3
  2. Impossible
  3. 2

Source

Waterloo local 2004.09.19

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

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

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

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

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

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

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

  1. #include<cstring>
  2. #include<string>
  3. using namespace std;
  4. void swap_palindrome(char s[],int begin,int end,int &step)
  5. {
  6. if(end-begin<=) return;
  7. int i,left_distance,right_distance;
  8. for(i=begin;i<end;i++) if(s[i]==s[end]) break;
  9. left_distance=i-begin;
  10. for(i=end;i>begin;i--) if(s[i]==s[begin]) break;
  11. right_distance=end-i;
  12. if(left_distance<right_distance){
  13. step+=left_distance;
  14. for(int i=left_distance+begin;i>begin;i--) swap(s[i],s[i-]);
  15. }else{
  16. step+=right_distance;
  17. for(int i=end-right_distance;i<end;i++) swap(s[i],s[i+]);
  18. }
  19. swap_palindrome(s,begin+,end-,step);
  20. }
  21. int is_palindrome(char s[])
  22. {
  23. int letter[]={},len=strlen(s);
  24. for(int i=;i<len;i++) letter[ (s[i]-'a') ]++;
  25. int count=;
  26. for(int i=;i<;i++){
  27. if(letter[i]%==) count++;
  28. }
  29. if(count>) return ;
  30. else return ;
  31. }
  32. int main()
  33. {
  34. int n;scanf("%d",&n);
  35. char s[];
  36. while(n--){
  37. scanf("%s",s);
  38. if(!is_palindrome(s)) printf("Impossible\n");
  39. else{
  40. int begin=,end=strlen(s)-,step=;
  41. swap_palindrome(s,begin,end,step);
  42. printf("%d\n",step);
  43. }
  44. }
  45. return ;
  46. }

另外……这题,刚开始我用了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. String s = new String("xyz");产生了几个对象?

    面试官Q1:请问String s = new String("xyz");产生了几个对象? 对于这个问题,老套路先上代码: public class StringTest { pu ...

  2. Mydumper介绍

    Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydumper. 一.Myd ...

  3. Ethereum Dapp Tutorial — Part 1

    在上一篇文章中,通过和传统的 web程序相比较解释了以太坊平台的结构.作为一个开发者,学习新技术的最好的方式就是构建一个玩具程序. 在这篇文章中我们将会构建一个简单的“hello word”程序,这个 ...

  4. linux每日命令(17):which命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...

  5. Django项目实战 - html中用户登录判断

    实现逻辑: {% if request.user.is_authenticated %} 个人信息{% else %}登录{% endif %} 直接上代码 {% if request.user.is ...

  6. 发展简史jQuery时间轴特效

    发展简史jQuery时间轴特效.这是一款鼠标滚动到一定的高度动画显示企业发展时间轴特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="wr ...

  7. 学习MongoDB(三) Add an Arbiter to Replica Set 集群中加入仲裁节点

    Add an Arbiter to Replica Set 在集群中加入仲裁节点,当集群中主节点挂掉后负责选出新的主节点,仲裁节点也是一个mongo实力,但是它不存储数据. 1.仲裁节点消耗很小的资源 ...

  8. 线程安全的ConcurrentQueue<T>队列

    队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...

  9. 如何解决安装VMware后郑广电宽带客户端不能登录的问题?

    如何解决安装VMware后郑广电宽带客户端不能登录的问题? 问题:安装VMware后,郑广电宽带客户端不能登录,提示:“不允许代理上网”. 解决:将VMware的虚拟网卡(VMnet1和VMnet8) ...

  10. debian系列下c++调用mysql, linux下面安装mysql.h文件

    mysql.h的报错还没有解决,你们不用看了,等我解决了吧还不知道什么时候 先用c吧 #include <stdio.h> #include <stdlib.h> #inclu ...