UVa 10716 - Evil Straw Warts Live
题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数。
如果字符串中出现奇数次的字母的个数>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的更多相关文章
- 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(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- poj 1854 Evil Straw Warts Live 变成回文要几次
Evil Straw Warts Live Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1799 Accepted: ...
- POJ 1854 - Evil Straw Warts Live
Description A palindrome is a string of symbols that is equal to itself when reversed. Given an inpu ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- POJ 1854 贪心(分治)
Evil Straw Warts Live Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1144 Accepted: ...
- 【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 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- 使用nodejs的http模块创建web服务器
使用nodejs的http模块创建web服务器 laiqun@msn.cn Contents 1. web服务器基础知识 2. Node.js的Web 服务器 3. 代码实现 1. web服务器基础知 ...
- 剑指offer 链表中倒数第K个节点
利用两个间隔为k的指针来实现倒数第k个 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- codeforces div2 677 D
http://codeforces.com/problemset/problem/677/D 题目大意: 给你一个n*m的图,上面有p种钥匙(p<=n*m),每种钥匙至少有一个,期初所有为1的钥 ...
- MusiCode 批量下载指定歌手的所有专辑(已解除验证码限制)
一直想把喜欢的歌手的专辑全都归类并下载下来,由于那专辑数量实在太多了,再加上最近开始学习python,就想着何不用python写个脚本把下载过程自动化呢?所以就花了点时间写了这么个东西,分享给有需要的 ...
- C++ 输出Cstring遇见的奇葩问题
先上代码 // webConteng.cpp : Defines the entry point for the console application. // #include "stda ...
- oracle中的赋权
1 怎么给用户赋权限 grant create view to scott; (create view 是权限的名称) 2 怎么给用户撤销权限 revoke create view from scot ...
- expected: file:///
[java] java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.190.128:9000/user/hadoop/output/ ...
- extjs入门
http://blog.csdn.net/xiebaochun/article/details/36414437
- CodeForces 614C Peter and Snow Blower
简单计算几何,只要算出圆心到多边形上的最短距离和最长距离即可 #include<cstdio> #include<cstring> #include<cmath> ...
- unity3d之在屏幕上画线
如何在屏幕上画线,简单的代码如下: using UnityEngine; public class Test : MonoBehaviour { void OnGUI() { GL.LoadOrtho ...