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. 如何寻找linux下相关软件

    当在linux下运行程序遇到找不到库的时候可以使用yum whatprovides 来找到到需要安装的包. 例如:yum whatprovides libz.so.1  然后安装找到的包 yum in ...

  2. 大型互联网架构概述 关于架构的架构目标 典型实现 DNS CDN LB WEB APP SOA MQ CACHE STORAGE

    大型互联网架构概述 目录 架构目标 典型实现 DNS CDN LB WEB APP SOA MQ CACHE STORAGE 本文旨在简单介绍大型互联网的架构和核心组件实现原理. 理论上讲,从安装配置 ...

  3. OpenLayers 官网例子的中文详解

    https://segmentfault.com/a/1190000009679800?utm_source=tag-newest 当你希望实现某种功能的时候,即使你对 openlayers 几乎一窍 ...

  4. Android UI系列-----RelativeLayout的相关属性

    本篇随笔将主要记录一些RelatieLayout的相关属性,并将猜拳游戏通过RelativeLayout实现出来 RelativeLayout的几组属性 第一组属性:android:layout_be ...

  5. 【Vegas原创】SQL Server 只安装客户端的方法

    只安装管理工具

  6. SAP BW: Replacement Path Variables

    How to use Replacement Path Variables to perform Date Calculations A Step-by-Step guide Have you eve ...

  7. 重定向如何携带cookie

    起因 最近在做微信开放平台,需要给第三方入住,而且入住方都有自己的二级域名.做过微信开发的人都知道,坑爹的是微信并不支持这种二级域名的方式,所以用一个域名专门来处理. 问题 然后由于采用了一个专门的域 ...

  8. 安装Node和NPM

      1.node和NPM是什么?   Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js ...

  9. Java知多少(100)图像处理基础

    图像是由一组像素构成,用二进制形式保存的图片.java语言支持GIF.JPEG和BMP这3种主要图像文件格式.java语言的图像处理功能被封装在Image类中. 图像载入和输出 在java程序中,图像 ...

  10. 关于Solaris 的磁盘的分区

    也许是深受LINUX  Windows  fdisk 影响,每次看完Solaris的format命令,总是云里雾里.我今天总结一下,各位给点指点 一. Linux.Windows  传统的磁盘区层级, ...