uva 10716 Evil Straw Warts Live(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢。代码能力还是得加强啊。思维
得缜密。不能想当然,要有根据,写上的代码要有精确度。省的以后还得慢慢调试
思路:贪心。每次都查看两端位置上的字母是否相等。若不相等就在里面查找能使他们相等且所需移动位置最少的那
个。然后交换。记录交换的距离,贪心的离最后一个由近及远找与第一个位置相等的。同理贪心从第一个位置找和最
后一个位置相等且离第一个位置近期的。
。
。感觉这样的方法确实能够,可是并不会证明这样的策略的正确性。。
。
代码:
#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(贪心回文串)的更多相关文章
- 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 ...
- poj 1854 Evil Straw Warts Live 变成回文要几次
Evil Straw Warts Live Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1799 Accepted: ...
- UVa 10716 - Evil Straw Warts Live
题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...
- 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 ...
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- POJ 1854 - Evil Straw Warts Live
Description A palindrome is a string of symbols that is equal to itself when reversed. Given an inpu ...
- Uva 11584,划分成回文串
题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- uva 10453 【回文串区间dp】
Uva 10453 题意:给定字符串,问最少插入多少个字符使其变成回文串,并任意输出一种结果. 题解:和Uva 10739类似,这里是只能增加.类似定义dp[i][j]表示子串Si...Sj变为回文串 ...
随机推荐
- C语言用指针输出数组里的值
因为*(arr)是指arr数组内存编号所对应的首地址的值,即arr[0]的值.所以用*(arr+i)可以实现遍历整个数组.
- 判断excel是否包含隐藏sheet
Workbook workbook =new XSSFWorkbook("D:\\文档1.xlsx"); System.out.println(workbook.isSheetHi ...
- 隐藏win10任务栏输入法M图标
在任务栏右键=>任务栏设置=>打开或关闭系统图标=>(关闭)输入指示
- elk 6.3.2 搭建
CentOS7和java1.8.0) 然后登陆elastic的官网地址下载ELK组件:https://www.elastic.co/cn/products 我是下载了6.3.0版本的: elast ...
- 王垠:写给支持和反对《完全用Linux工作》的人们
王垠:写给支持和反对<完全用Linux工作>的人们 在一阵阵唾骂和欢呼声中,<完全用linux工作>被转载到了天涯海角.很多人支持我,也有很多人唾骂我.我不知道它是怎样流传到那 ...
- Kattis - missinggnomesD Missing Gnomes (思路题)
题目: 题意: 给出已经去除了几个数的一个序列,任务是将去除的数字插回去补全这个序列,输出字典序排在第一的那个补全的序列. 例如: 样例输入: 5 3 1 4 2 样例输出: 1 3 4 2 5 思路 ...
- mysql (5.7版本)---的配置
1.去到官方网站下载 https://dev.mysql.com/downloads/installer/ 或者直接下载 --> https://dev.mysql.com/get/Down ...
- Python数据库连接池DBUtils(基于pymysql模块连接数据库)
安装 pip3 install DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: # BDUtils数据库链接池: 模式一:基于threaing ...
- APUE 文件IO
文件 IO 记录书中的重要知识和思考实践部分 Unix 每个文件都对应一个文件描述符(file descriptor),为一个非负整数,一个文件可以有多个fd, 后面所有与文件(设备,套接字等)有关操 ...
- [spoj1182][Sorted Bit Sequence] (数位dp)
Description Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ ...