这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢。代码能力还是得加强啊。思维

得缜密。不能想当然,要有根据,写上的代码要有精确度。省的以后还得慢慢调试

思路:贪心。每次都查看两端位置上的字母是否相等。若不相等就在里面查找能使他们相等且所需移动位置最少的那

个。然后交换。记录交换的距离,贪心的离最后一个由近及远找与第一个位置相等的。同理贪心从第一个位置找和最

后一个位置相等且离第一个位置近期的。

。

。感觉这样的方法确实能够,可是并不会证明这样的策略的正确性。。

。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<algorithm> using namespace std;
int a[30];
string s;
void swap1(int x,int y)
{
char ch = s[x];
for(int i=x; i<y; i++)
{
s[i] = s[i+1];
}
s[y] = ch;
return ;
}
void swap2(int x,int y)
{
char ch = s[y];
for(int i=y; i>x; i--)
{
s[i] = s[i-1];
}
s[x] = ch;
return ;
}
int cost;
void solve()
{
int i,j;
for(i=0; i<(s.size()/2); i++)
{
//cout << "i = "<<i << endl;
if(s[i]!=s[s.size()-1-i])
{
for(j=1; j<=s.size()-1-1-i-i; j++)
{
if(s[j+i]== s[s.size()-1-i])//推断是否和最后一个相等。若相等就放到对称位置
{
swap2(i,i+j);//准确写出移动函数
//cout << s << endl;
cost += j;//移动距离
break;
}
else if(s[s.size()-1-i-j] == s[i])//推断是否和第一个相等。若相等就和最后一个互换。即它的最后一个位置(这里所说第一个位置是当前推断的位置不是所有序列的第一个)
{
swap1(s.size()-1-i-j,s.size()-1-i);//注意调用的移动函数是否可行
//cout << s << endl;
cost+= j;
break;
}
}
}
}
return ;
}
int main()
{
int T,i;
int ans;
cin >> T;
getchar();
while(T--)
{
ans = 0;
memset(a,0,sizeof(a));
s.clear();
cin >> s;
getchar();
for(i=0; i<s.size(); i++)
{
a[s[i]-'a']++;
}
int flag = 0;
for(i=0; i<26; i++)//预处理能否够构成回文数
{
if(a[i]%2!=0)
{
ans++;
}
}
cost = 0;
if(ans > 1)
cout << "Impossible" << endl;
else
{
solve();
cout << cost << endl;
}
}
return 0;
}

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. poj 1854 Evil Straw Warts Live 变成回文要几次

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

  3. UVa 10716 - Evil Straw Warts Live

    题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...

  4. UVA 12378 Ball Blasting Game 【Manacher回文串】

    Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...

  5. Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串

    题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...

  6. POJ 1854 - Evil Straw Warts Live

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

  7. Uva 11584,划分成回文串

    题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...

  8. UVA - 11584 划分字符串的回文串子串; 简单dp

    /** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...

  9. uva 10453 【回文串区间dp】

    Uva 10453 题意:给定字符串,问最少插入多少个字符使其变成回文串,并任意输出一种结果. 题解:和Uva 10739类似,这里是只能增加.类似定义dp[i][j]表示子串Si...Sj变为回文串 ...

随机推荐

  1. redis之有序集合类型(Zset)——排行榜的实现

    当数据库对排序支持的不是很好,可以利用redis有序集合排序 原文链接:http://blog.csdn.net/loophome/article/details/50373202

  2. UVM基础之---------uvm factory机制register

    factory机制的一大特点就是根据类的名字来创建类的实例. factory 机制中根据类名来创建类的实例所用到的技术:一是参数化的类,二是静态变量和静态函数.这两者是factory机制实现的根本所在 ...

  3. datetimebox赋值或取值

    datetimebox赋值或取值 $('#j_dateStart').datebox('setValue', ""); //赋予空值 $("#j_dateStart&qu ...

  4. react native 从头开始

    1.react-native run-android 报错SDK location not found. Define location with sdk.dir in the local.prope ...

  5. 继 S-HR之代码创建临时表并插入数据 完整功能之员工职业信息变更报表

    目的示例1: 制作员工职业信息报表[S-HR系统的报表其实就是列表o.0,醉了] EcirrWithPP.js shr.defineClass("shr.custom.EcirrWithPP ...

  6. spring cloud (一):大话 Spring Cloud

    转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...

  7. css3属性之-webkit-margin-before

    当没有对浏览器进行css边距初始化时,在web-kit浏览器上会出现下面的浏览器默认边距设置: ul, menu, dir { display: block; list-style-type: dis ...

  8. 服务器做ssh免秘钥登陆

    集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...

  9. 洛谷 1017 进制转换 (NOIp2000提高组T1)

    [题解] 纯模拟题. 我们都知道十进制数化成m进制数可以用短除法,即除m取余.逆序排列.而m进制数化为十进制数,按权展开求和即可. 但在本题中进制的基数R可能为负数,我们知道a%R的符号与R一致,也就 ...

  10. 恶补---bell数

    定义 bell数即一个集合划分的数目 示例 前几项的bell数列为 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 ,... 求值方法 1.bell ...