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. Django Web开发学习笔记(4)

    第四章 模板篇 上一章的内容,我们将HTML的代码和Python代码都混合在了在view.py的文件下.但是这样做的坏处无疑是明显的,引用DjangoBook的说法: 对页面设计进行的任何改变都必须对 ...

  2. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  3. 设置tomcat 编译文件位置【转】

    问题: 将项目发布到tomcat时,发现tomcat的cclasses目录下无任何编译后的文件. 解决方法:设置MyEclipse的文件编译目录即可: http://my.oschina.net/u/ ...

  4. 在 word 中对正文和目录进行分节显示页码

    使用版本 word 2016 使目录独占一页:在正文第一页的第一个字符前插入分节符下一页(布局--分节符--下一页),此时会在正文第一个字符前插入分节符.在之前插入一张空白页,用于插入目录.(插入 - ...

  5. 2018年末--积极拥抱h5.转载 大前端时代来临,我们何去何从?

    1.大前端时代是什么? 大前端时代是WEB统一的时代,利用html5或者6甚至7,不但可以开发传统的网站,做炫酷的网页动态效果,更可以采用BS架构应用程序.开发手机端web应用.移动端Native应用 ...

  6. 菜鸟学SSH(十九)——提高用户体验之404处理

    只要做过WEB开发人对于“404”已经再熟悉不过了吧.当我们访问的资源不存在时,它就会跑出来跟你打招呼啦.但是默认情况下,404页面比较简陋,不是很友好.而且一般用户不知道404是个神马东东,还以为是 ...

  7. 【Linux高级驱动】触摸屏工作原理与工作流程

    触摸屏工作原理 触摸屏工作流程 @成鹏致远 (blogs:http://lcw.cnblogs.com) (email:wwwlllll@126.com) ) From WizNote

  8. Git 更新操作

    修改现有函数 Tom 执行克隆操作后,看到新的文件string.c,他想知道这个文件到存储库?目的是什么?于是,他执行 git 日志命令. [tom@CentOS ~]$ git clone gitu ...

  9. Java知多少(12)运算符

    Java中的运算符和C/C++相差无几. 数学运算符 数学运算,结果为一个数值.见下表: 运算符 说明 举例 + 加法 1 + 2 - 减法 4 - 3.4 * 乘法 7 * 1.5 / 除法 3.5 ...

  10. 注解实现Bean依赖注入

    12.2.1  概述 注解实现Bean配置主要用来进行如依赖注入.生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的数据将覆盖基于注解配置中的依赖注入的数 ...