Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1799   Accepted: 523

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 题意:一个字符串变成回文最少要几次 题解:
1、用回文的性质判断是否可以变成回文:个数为奇数的字母出现的次数小于等于1
2、从字符串的两端出发(l,r),在字符串两端同时找能使s[l]==s[r]的位置,
3、取使左右两端相等步数最少的那一端,并从内到外交换位置(若从外到内交换会疯狂WA)
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
int vis[];
string s; int check(string ss)
{
int cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<ss.length();i++)
vis[ss[i]-'a']++;
for(int i=;i<;i++)
{
if(vis[i]%==)
cnt++;
}
if(cnt>)
return ;
else
return ;
}
int main()
{ int n,ans;
cin>>n;
while(n--)
{
ans=;
cin>>s;
if(check(s))
cout<<"Impossible"<<endl;
else
{
int len=s.length(),l,r,j,mid,x=MAX,y=MAX; for(int i=;i<len/;i++)
{
l=i;
r=len--i;
if(s[l]!=s[r])
{
for(j=l+;s[j]!=s[r];j++);
x=j;
for(j=r-;s[j]!=s[l];j--);
y=j;
if(x-l>r-y)
{
ans=ans+r-y;
for(int j=y;j<r;j++)
s[j]=s[j+];
}
else
{
ans=ans+x-l;
for(int j=x;j>l;j--)
s[j]=s[j-];
}
}
}
cout<<ans<<endl; }
}
return ;
}

poj 1854 Evil Straw Warts Live 变成回文要几次的更多相关文章

  1. POJ 1854 - Evil Straw Warts Live

    Description A palindrome is a string of symbols that is equal to itself when reversed. Given an inpu ...

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

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

  3. 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 ...

  4. UVa 10716 - Evil Straw Warts Live

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

  5. (字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)

    /* * POJ_1159.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> # ...

  6. POJ 1854 贪心(分治)

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

  7. hdu 3068 最长回文 manacher算法(视频)

    感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...

  8. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  9. POJ 3974 回文串-Manacher

    题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...

随机推荐

  1. 【转】路由转发过程的IP及MAC地址变化

    A-----(B1-B2)-----(C1-C2)-------E 就假设拓扑图是这个样子吧,B1和B2是路由器B上的两个接口,C1和C2是路由器C上的两个接口,A和E是PC,由主机A向主机E发送数据 ...

  2. c++将字符转换成字符串

    转载:https://blog.csdn.net/dididisailor/article/details/83189135 char c; string str; stringstream stre ...

  3. 二、linux基础-路径和目录_用户管理_组_权限

    2.1路径和目录1.相对路径:参照当前目录进行查找.   如:[root@localhost ~]# cd ../opt/hosts/备注:相对路径是从你的当前目录开始为基点,去寻找另外一个目录(或者 ...

  4. jsp页面展示更加商品的分类,控制商品的显示

    我的大概思路是这样的,第一后果获取所有的商品分类 保存在list集合里面,第二从后台获取所有的商品 第三在JSP页面遍历商品分类集合放在页面的左边,然后jsp页面商品详细信息这块,也得先遍历商品分类, ...

  5. Linux centosVMware mysql用户管理、常用sql语句、mysql数据库备份恢复

    一.mysql用户管理 grant all on *.* to 'user1'@‘127.0.0.1’ identified by 'mimA123'; 创建user1用户 使用user1登录 /us ...

  6. 使用eclipse创建一个简单的Java Web应用程序

    关于Java JDK/JRE.Tomcat的配置等等都没什么好说的,主要记录一下使用Eclipse创建web工程时的一些点以及说一说自己用IDEA的创建失败的过程(IDEA没运行成功...暂时不想弄了 ...

  7. FFmpeg——命令笔记

    1. 获取 dshow设备列表 ffmpeg -list_devices true -f dshow -i dummy 2. 通过UDP流推ts文件: ffmpeg.exe -re -i zhen.t ...

  8. Python 中的else

    在其他程序语言中,else 似乎只是与 if 关键字有缘分.而与其他的关键字没有联系,不能搭配使用,而在python中,else 除了与 if 匹配外, 还可以与for.while/ try等关键字匹 ...

  9. LibreOJ #2006. 「SCOI2015」小凸玩矩阵

    想了挺久没想出来,一看题解恍然大悟.一个数对应一行和一列,二分答案,凡是小于等于答案的就连边.如果满足能够取出 \(n - k + 1\) 个不比二分中点 \(mid\) 大的数,那么r = mid, ...

  10. Java单例模式:为什么我强烈推荐你用枚举来实现单例模式

    单例模式简介 单例模式是 Java 中最简单,也是最基础,最常用的设计模式之一.在运行期间,保证某个类只创建一个实例,保证一个类仅有一个实例,并提供一个访问它的全局访问点.下面就来讲讲Java中的N种 ...