uva-10716-贪心
题意:输入长度在100内的小写字母的字符串,求把它变成回文字符串的最少交换次数.如果不能变成回文串,输入,Impossible.
解法:
要变成回文字符串,必须满足一个性质,所有的字符出现次数都是偶数,或者只有一个字符是奇数,
每次取俩端的字符,计算一下,那个花费低就选那个,注意,如果字符串长度是奇数,并且俩端某个字符应该是放在中间的位置,那么它的花费肯定是无穷大.
可以证明,如果我们s1.....e1......s2.....e2能够保证从s1-e1,e2-s2都是回文的字符串,那么放在中间的那个字符最后肯定会在中间.
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset> namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::bitset; constexpr int N = ; string str;
bool check()
{
int a[] = { };
int odd = ;
for (int i = ;i < str.length();i++)
{
a[str[i] - 'a']++;
}
for (int i=;i<;i++)
{
if (a[i] % == )
{
odd++;
if (odd == )
return false;
}
}
return true;
}
void solve()
{
int n;
cin >> n;
while (n--)
{
cin >> str;
if (!check())
{
cout << "Impossible" << endl;
continue;
}
int s = , e = str.length()-;
int total = ;
//e是最后一个元素
while (s < e)
{
if (str[s] == str[e])
{
++s;
--e;
continue;
}
int index1 = -;
int s1 = ;
for (int i=e;i > s; i--)
{
if (str[i] == str[s])
{
index1 = i;
break;
}
}
if (index1 == -)
{
s1 = INT32_MAX;
}
else
s1 = e - index1;
int index2 = -;
int s2 = -;
for (int i=s;i < e;i++)
{
if (str[i] == str[e])
{
index2 = i;
break;
}
}
if (index2 == -)
{
s2 = INT32_MAX;
}
else
s2 = index2 - s;
if (s2 < s1)
{
//select e
for (int i=index2;i>s;i--)
{
swap(str[i],str[i-]);
++total;
}
}
else
{
//select s
for (int i=index1;i<e;i++)
{
swap(str[i],str[i+]);
++total;
} }
--e;
++s;
}
cout << total << endl;
}
} }; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve();
return ;
}
uva-10716-贪心的更多相关文章
- 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(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
- UVa 1467 (贪心+暴力) Installations
题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...
- UVa 10716 - Evil Straw Warts Live
题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...
- Party Games UVA - 1610 贪心
题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
随机推荐
- MS Batch AI
微软的Batch AI服务是一项新服务,它可以帮助你在GPU pool上训练和测试机器学习模型,包括深度学习模型.它简化了在当前许多流行的深度学习框架(如TensorFlow.Microsoft认知工 ...
- js this详解,事件的三种绑定方式
this,当前触发事件的标签 在绑定事件中的三种用法: a. 直接HTML中的标签里绑定 onclick="fun1()"; b. 先获取Dom对象,然后利用dom对象在js里绑定 ...
- 源代码安装Apache、Mysql、PHP
源代码软件的优点: 获得最新版,能及时修复bug: 能自行修改和定制: 源代码打包形式: .tar.gz和.tar.bz2格式居多: 完整性校验: md5sum校验工具 ...
- ef 吐糟
方法:从dbcontext到objectcontext DbContext及其相关的API是EF4.1中一大新特性.简单说,DbContext就是之前EF的核心类ObjectContext的抽象封装. ...
- Ubuntu 14.04 下安装 MongoDB 服务器 和 PHP MongoDB Driver 数据驱动
https://laravel-china.org/topics/309/install-mongodb-server-and-php-mongodb-driver-data-driver-under ...
- js copy数组 对象
js copy数组 slice concat 浅拷贝 copy 对象 Object.assign({},obj); es6 ie要用babel转 暴力copy 用JSON.parse(JSON. ...
- [转][C#]降级.net 源码4.5
来自:https://www.cnblogs.com/had37210/p/8057042.html 主要是 Task 的降级: 1.net4.5.2 引入了 async/await 关键字. 这个其 ...
- [转][PowerShell]ps执行重启IIS
来自:https://www.aliyun.com/jiaocheng/871477.html write-output 'Restarting IIS servers ............... ...
- centos7.0KVM虚拟化
需要在图形化界面完成实验 查看CPU信息 # cat /proc/cpuinfo centos7默认安装的虚拟化包 # yum list open*tools 如果没安装就安装 #yum instal ...
- [UE4]死亡后调整视角
AddLocalOffset:本地坐标偏移. 脱离控制器操作 会影响“OnDie”方法里面的相机移动操作,而函数里面又不允许使用“Delay”方法,但可以使用“Set Timer By Functio ...