poj 1854 Evil Straw Warts Live 变成回文要几次
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 1799 | Accepted: 523 |
Description
swap "ad" to yield "mamda"
swap "md" to yield "madma"
swap "ma" to yield "madam"
Input
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
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 变成回文要几次的更多相关文章
- POJ 1854 - Evil Straw Warts Live
Description A palindrome is a string of symbols that is equal to itself when reversed. Given an inpu ...
- uva 10716 Evil Straw Warts Live(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- 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 ...
- UVa 10716 - Evil Straw Warts Live
题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...
- (字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)
/* * POJ_1159.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> # ...
- POJ 1854 贪心(分治)
Evil Straw Warts Live Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1144 Accepted: ...
- hdu 3068 最长回文 manacher算法(视频)
感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...
- POJ 1159 回文串-LCS
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...
- POJ 3974 回文串-Manacher
题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...
随机推荐
- Jquery 分页案例
Jquery 分页案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...
- jsoup教学系列
http://my.oschina.net/flashsword/blog?catalog=390084
- uniGUI 应用程序体系结构(11)
下图表示 uniGUI 服务器的内部结构. 每个 uniGUI 服务器都有一个ServerModule的副本, 每台服务器创建一次, 同时根据用户活动动态创建和销毁多个Session. uniGUI ...
- Linux之系统优化配置
Linux系统优化配置 更新国内镜像源 国内速度快的常用更新源如下: http://mirrors.sohu.com http://mirrors.163.com [root@greymous ...
- 【转载】XShell 连接 VirtualBox CentOS7
1.安装 XShell 网址:http://sw.bos.baidu.com/sw-search-sp/software/07a1d9cec0638/Xshell-5.0.1339.exe 尽量不要安 ...
- 图片IO域 旋转画面的组态 图片是4个静止的风扇 PLC的MW6为风扇指针..
图片IO域 旋转画面的组态 图片是4个静止的风扇 PLC的MW6为风扇指针.. Plc在循环中断组织块 OB35 中 将MW6 每100ms 加1 加到4 清0 图片[MW6] MW6 是图片指针 对 ...
- 程序启动:Serialize奋斗史
1.前方高能 在这电子与数据风驰电掣的世界里,人们一刻不停歇的工作着. 但是我却是一个被冷落的人, 因为我做的工作最近用的人太少了.大多数时候,我只能羡慕的看着线程.反射.注解.集合.泛型这些明星员工 ...
- Fiddler抓取HTTPS
对于想抓取HTTPS的测试初学者来说,常用的工具就是fiddler.可是在初学时,大家对于fiddler如何抓取HTTPS真是伤了脑筋,可能你一步步按着网上的帖子成功了,那当然是极好的. 有可能没有成 ...
- redis学习笔记-03:redis安装
一.redis的安装和配置 1.下载redis-5.0.4.tar.gz到/opt目录下,解压命令 :tar -zxvf redis-5.0.4.tar.gz,解压后出现redis-5.0.4的文件夹 ...
- Calendar 时间类的应用
Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calendar 类进行时间和日期的处 ...