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. 配置Slf4j依赖,桥接各种多个日志组件(排除commons-logging依赖的影响)

    由于各个jar组件使用的日志框架不一样,实际项目中可能会引入多个jar,通常使用的日志框架有 commons-logging log4j 若同一个项目引入多个日志组件,那么Slf4j组件会有不能捕捉到 ...

  2. intellijidea课程 intellijidea神器使用技巧2-2 精准搜索

    高效定位: 1 类: 类的跳转: Ctrl shift n ==> 查询类名 Ctrl shift n n ==> jar包中的类 2 文件: Ctrl shift shift n ==& ...

  3. 企业工商营业执照副本模板PSD源文件素材下载

    企业工商营业执照副本PSD模板下载地址: http://www.qijieworld.com/thread-1911181-1-1.html 模板为psd格式内容可编辑修改,需使用 Photoshop ...

  4. ArcGIS Runtime SDK for Android 各版本下载地址

    ArcGIS Runtime SDK for Android各版本下载地址:ArcGIS Runtime SDK交流群:249819194 SDK包中主要包含以下内容: 其中里面比较重要的有以下几项: ...

  5. Azure 进阶攻略 | 上云后的系统,「门禁」制度又该如何实现?

    各位办公室白领们,不妨回想一下自己每天去公司上班时的一些细节. 为避免「闲杂人等」进入工作场所,我们需要证明自己是这家公司的员工才能进入,对吧!所有员工,无论所属部门或职位,都必须先证明自己身份,例如 ...

  6. 了解Web及网络基础(二)

    HTTP报文分为两种,HTTP请求报文跟HTTP响应报文. HTTP请求报文的结构如下: 其中,请求行中包括的内容有方法.URI和HTTP版本,请求首部字段.通用首部字段和实体首部字段隶属于HTTP首 ...

  7. linux系统管理命令kata练习

    磁盘管理 #打印当前目录下,各个文件大小和目录的磁盘空间占用情况. #获取硬盘被占用了多少空间,目前还剩下多少空间等信息 df -lh #查看管理磁盘分区 fdisk -l #制作文件系统 mkfs ...

  8. [转载]互联网 免费的WebService接口

    股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebServices/St ...

  9. v-for的深层用法

    为了提升循环的性能,我们会给循环加上一个唯一的key值,这个key值一定是唯一的 <div id='root'> <div v-for='(item,index) of list' ...

  10. 1.4 NBU配置备份策略(Policy)

    1.4 配置备份策略(Policy) 一个备份策略由四部分组成. Attributes(属性) Policy是否Active Policy类型 由此Policy产生的任务的优先级 使用的Storage ...