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> # ...
随机推荐
- swoole 消息队列
<?php /** * 场景: * 监控订单表状态 队列通信 * 一个进程向队列发布消息 另外两个进程争抢 */ //设置主进程名 echo '主进程id:' . posix_getpid() ...
- ApacheDbUtilsUpdate
ApacheDbUtilsUpdate package p1; import com.DataSourceUtil; import org.apache.commons.dbutils.QueryRu ...
- Servlet 设置字符编码filter
filter package common; import javax.servlet.*; import java.io.IOException; public class EncodingFilt ...
- MongoDB基础篇1:安装和服务配置
一.下载 请前往官网下载community版本MongoDB,我当前可见最新版本是3.6.4 https://www.mongodb.com/download-center#community 如需下 ...
- C语言的常用的数据类型有哪些_所占字节分别是多少
整型 整形打印使用%d short:短整型,占16位,2个字节 int:占32位,4个字节 long:长整型,占4个字节,本来意思比int更多,但是目前来看基本都是和int一样 浮点型 浮点型计算会影 ...
- 你知道Verilog HDL程序是如何构成的吗
本节通过硬件描述语言Verilog HDL对二十进制编码器的描述,介绍Verilog HDL程序的基本结构及特点. 二十进制编码器及Verilog HDL描述 二十进制编码器是数字电路中常用的电路单元 ...
- Shenandoah 与 ZGC
简介 Shenandoah GC 与 ZGC 同为新一代的低延迟收集器, 分别由RedHat和Oracle开发, 目前还在实验阶段, 尚未使用于生产环境. GC的三项指标: Footprint(内存占 ...
- 攻防世界web进阶区(1)
1.题目地址:http://111.198.29.45:43589 页面提示打开robots文件,则: 页面有提示输入fl0g.php,那么 获取flag. 2.题目地址:http://111.198 ...
- English-Number
English-Number 1. 基数与序数 2. 数量级前缀 3. 与数量级前缀连用的常用单位 4. 数量的完整写法式例 5. 数学符号 6. 其他常用单位 7. 数字的用法 7.1 日期时间 7 ...
- Lesson 2 Spare that spider
How much of each year do spiders killing insects? Why, you may wonder, should spiders be our friends ...