题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数。

  如果字符串中出现奇数次的字母的个数>1,则不能构成回文。然后...就没思路了...看网上说用贪心的思想先从两端开始考虑,决定两端的字母后再缩小问题范围直至字符串长度<=2。可是看网上的代码都是只考虑了两端字母,而没有考虑其他可能的情况,如mdcfamcda,如果两端都变为m的话需要交换3次,两端都变为a的话需要交换4次,可是如果变为d的话只需要交换两次,但是网上的代码没考虑这种情况竟然AC了,为什么?是测试数据的问题还是其中有什么我没看出来的东西?

 #include <cstdio>
#include <cstring> int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
char str[];
while (T--)
{
scanf("%s", str);
int cnt[] = {};
for (int i = ; str[i] != '\0'; i++)
cnt[str[i]-'a']++;
int t = ;
for (int i = ; i < ; i++)
if (cnt[i] % ) t++;
if (t > )
{
printf("Impossible\n");
continue;
}
int ans = ;
for (int s = , e = strlen(str)-; s < e; s++, e--)
{
if (str[s] != str[e])
{
int p = s;
while (str[p] != str[e]) p++;
int q = e;
while (str[q] != str[s]) q--;
if (p - s < e - q)
{
ans += p-s;
for (int i = p; i > s; i--)
str[i] = str[i-];
}
else
{
ans += e-q;
for (int i = q; i < e; i++)
str[i] = str[i+];
}
}
}
printf("%d\n", ans);
}
return ;
}

UVa 10716 - Evil Straw Warts Live的更多相关文章

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

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

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

  3. poj 1854 Evil Straw Warts Live 变成回文要几次

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

  4. POJ 1854 - Evil Straw Warts Live

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

  5. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. POJ 1854 贪心(分治)

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

  8. 【UVA 11383】 Golden Tiger Claw (KM算法副产物)

    Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gong Wu. But EvilBoy Geni ...

  9. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

随机推荐

  1. hdu_5719_Arrange(脑洞题)

    题目连接:hdu_5719_Arrange 题意: 给你1-n这 n个数,设一个排列的第i个数为Ai, Bi为A1到Ai的最小值,Ci为C1到Ci的最大值,问你有多少种排列方式,然后输出取模后的答案 ...

  2. 交换机VLAN、 TRUNK 、VTP 配置

    交换机VLAN. TRUNK .VTP 配置 1. 配置 CISCO 二层交换机的IP 地址(catalyst 2950 为例) SW1(config)#int vlan 1 //进入管理接口inte ...

  3. javascript 按位或(|),无符号右移(>>>)运算,组合技巧来实现————密码强度提示,四种情况??

    直接上代码,原来的代码中,switch中的第一个case,判断之后,少加了个break 跳出判断语句,害得我查了半天,“怎么样式老是不对,不科学啊,呵呵,原来是没跳出case的判断了,还会执行后面的判 ...

  4. iOS的基本图形绘制

    绘图的步骤: 1.获取上下文 2.创建路径(描述路径) 3.把路径添加到上下文 4.渲染上下文 通常在- (void)drawRect:(CGRect)rect这个方法里面绘制图形 为什么要再draw ...

  5. docker 1.12 版本 docker swarm 集群

    博客已经迁移到 个人博客中 个人博客 更新地址: http://www.xf80.com/2016/10/25/docker-swarm-1.12/ docker 1.12 版本 的新特性 (1)do ...

  6. 使用eclipse和maven一步一步配置web项目

    http://www.blogjava.net/kevonz/archive/2012/07/08/382542.html

  7. 轻量级sqlite是增删改查

    --创建数据库 create database ios --使用数据库 use ios --创建数据表 create table student ( stuid int primary key aut ...

  8. Docker Swarm集群

    Docker Swarm集群 IP 10.6.17.11  管理节点 IP 10.6.17.12   节点A IP 10.6.17.13   节点B IP 10.6.17.14   节点C 安装 Sw ...

  9. find命令--xargs--exec

    find命令 1.1.find命令选项-name 按照文件名查找-perm 按照文件权限来查找-prune 可以使用find命令排除当前文件夹,不查找-user 可以按照文件属主来查找-group 可 ...

  10. selenium 多线程

    http://www.cnblogs.com/dingmy/p/3438084.html