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

  如果字符串中出现奇数次的字母的个数>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. HTTP代理浅说

    简单的说HTTP代理就是处于HTTP客户端和服务器端之间,中转消息的中间人. 一种代理是代客户端去请求服务器,叫做Forward Proxy正向代理:另一种是代理真正的服务器来接收用户请求,叫做Rev ...

  2. Linux查看端口被哪个进程占用

    netstat -lnp|awk 'BEGIN{prt=":443$"}{if ($4 ~ prt) print $0}' 查看443端口

  3. Developing Backbone.js Applications

    https://addyosmani.com/backbone-fundamentals/

  4. Android 禁止屏幕休眠和锁屏的方法

    Introduction 常常我们开 发程序的时候我们不需要系统唤醒系统锁屏功能,比如我们在做xxxNowTV或XXX播放器这样的程序,用户有时候在看电视或视频的时候不希望系统的锁屏 功能启动,既不想 ...

  5. new del 问题

    实验一: new_del_caller工程(静态库) -new_del_caller.cpp #include <new> void new_del_caller() { ]; delet ...

  6. JS基础知识——定时器

    1.定时器——setInterval("执行函数名",间隔时间); setInterval(fun,1000);每隔1秒钟执行一次fun函数 setInterval("f ...

  7. LVS详解

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  8. 卷积神经网络(CNN)新手指南 1

    http://blog.csdn.net/real_myth/article/details/52273930 卷积神经网络(CNN)新手指南 2016-07-29 18:22 Blake 1条评论 ...

  9. Java 内存回收机制 -说到点上了

    下面这个图,很清楚地说明对象在new的时候是怎样开辟内存空间的 其中对象new出来的,是栈内存,变量的开辟是堆内存 Java的一个重要优点就是通过垃圾收集器GC (Garbage Collection ...

  10. nginx 防 webshell 跨目录

    对于在一台服务器上有多个虚拟主机的人来说,Apache有一个很好用的地方---配置php_admin_value,在里面配置一下 open_basedir就可以了.   但是Nginx却没有这样的设置 ...