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

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

题意:给你一个字符串,求最少交换相邻字符多少次能将它变为回文串。
思路:先处理两边的,然后删掉,递归操作。
代码:
 #include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"ctime"
#include"iostream"
#include"cstdlib"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
int ans;
int c[];
char s[];
void swap(char &e,char &f){
char ch;
ch=e,e=f,f=ch;
}
int dfs(int l,int r)
{
if(l>=r) return ans;
int lp=N,rp=-N;
for(int i=l;i<r;i++){//移动字符到左边与右边配对
if(s[i]==s[r]){
lp=i;
break;
}
}
for(int i=r;i>l;i--){//移动字符到右边与左边配对
if(s[i]==s[l]){
rp=i;
break;
}
}
if(lp-l<=r-rp){
ans+=lp-l;
for(int i=lp;i>l;i--) swap(s[i],s[i-]);
}
else{
ans+=r-rp;
for(int i=rp;i<r;i++) swap(s[i],s[i+]);
}
return dfs(l+,r-);
}
int main()
{
int n;
ci(n);
while(n--)
{
cin>>s;
memset(c,, sizeof(c));
int len=strlen(s);
for(int i=;s[i];i++) c[s[i]-'a']++;
int ok=;
for(int i=;i<;i++) if(c[i]&) ok++;
ans=;
if(ok>) puts("Impossible");
else pi(dfs(,len-));
}
}

POJ 1854 贪心(分治)的更多相关文章

  1. CF448C Painting Fence (贪心分治)

    题面 \(solution:\) 一道蛮水的分治题,但思想很不错(虽然我还是非常天真的以为是积木大赛原题,并且居然还有30分) 看到这个题目,根据贪心的一贯风格,我们肯定能想到将整个栅栏的下面某部分直 ...

  2. XDU1024简单逆序对(贪心||分治)

    题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...

  3. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  4. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  5. POJ 1741 [点分治][树上路径问题]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一棵有n个节点的树,每条边都有一个正权值,求一共有多少个点对使得它们之间路的权值和小于给定的k. 思路: <分治算法在树的路径问题中的应用 ...

  6. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

  7. poj 1328 贪心

    /* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...

  8. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  9. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

随机推荐

  1. FAT12格式的引导区实现

    org 07c00h ;================================================ jmp short START nop ; 这个 nop 不可少 ;这个结构将 ...

  2. RDF类型报表-PDF中文乱码

    在Oracle R12中,遇到了客户一张客户化的报表: 报表的输出格式是布局在RDF文件(非RTF)中,在并发请求输出时,PDF会出现中文乱码,而HTML和excel显示正常: 根据资料: 查看$OA ...

  3. Android - 开发页面需了解的dip,sp,px知识,以及它们的转换

    工作中,时常会有任务要求开发新页面,这时一般的流程是产品经理确定要开发的页面和功能,然后设计师提供设计稿,之后由我们开发人员完成开发工作. 通常,设计师提供的设计稿尺寸标注会很详细,例如涉及到字时,字 ...

  4. php的yii框架开发总结8

    EMailer是一个简单的封装PHPMailer类.利用这个扩展可以实现发邮件的功能. 下载地址:http://www.yiiframework.com/extension/mailer/ 下载解压把 ...

  5. IDEA下通过Git实现代码管理

    IDEA下通过Git实现代码管理 1.介绍 1.1 Git概述 Git是类似于SVN等代码管理软件,使用分布式技术实现.Github是互联网代码仓库,每个人可以在上面创建自己的仓库,使用git完成同g ...

  6. 梦织未来Windows驱动编程 第03课 驱动的编程规范

    最近根据梦织未来论坛的驱动教程学习了一下Windows下的驱动编程,做个笔记备忘.这是第03课<驱动的编程规范>. 驱动部分包括基本的驱动卸载函数.驱动打开关闭读取写入操作最简单的分发例程 ...

  7. 在WINDOWS下安装PEAR

    一.从官网下载go-pear.phar 文件,地址http://pear.php.net/go-pear.phar 将下载好的文件go-pear.phar 放到PHP安装目录下,dos 命令下进入PH ...

  8. JavaRebel 2.0 发布,一个JVM插件

    JavaRebel是一个JVM插件(-javaagent),能够即时重载java class更改,因此不需要重新部署一个应用或者重启容器,节约开发者时间. JavaRebel 2.0的新特征: 改变了 ...

  9. POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图

    题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...

  10. POJ-3190 Stall Reservations---优先队列+贪心

    题目链接: https://vjudge.net/problem/POJ-3190 题目大意: 有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶.挤奶的时候奶牛一定要单独放 ...