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 reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a palindrome. By swap we mean reversing the order of two adjacent symbols. For example, the string "mamad" may be transformed into the palindrome "madam" with 3 swaps:
- swap "ad" to yield "mamda"
- swap "md" to yield "madma"
- swap "ma" to yield "madam"
The first line of input gives n, the number of test cases. For each test case, one line of input follows, containing a string of up to 100 lowercase letters. Output consists of one line per test case. This line will contain the number of swaps, or "Impossible" if it is not possible to transform the input to a palindrome.
Sample Input
- 3
- mamad
- asflkj
- aabb
Output for Sample Input
- 3
- Impossible
- 2
题意:给定一些字符串,要求出能否通过交换字母变换为回文。。如果可以输出最少变换次数。。
思路:贪心。。
1、先判断能不能变换为回文。。如果字符串中没有或只有1个字母是奇数。就可以组成。。
2、每次从第一个字母开始。从后往前找到一个相同字母。放到最后就是匹配了。。每次移动的次数为当前位置到最后的位置的距离。
要注意有单个字母为奇数的情况。。最后要把这个字母另外拿出来移到最中间。。一开始没考虑这个wa了- -
代码:
- #include <stdio.h>
- #include <string.h>
- int t, len, sum, judge, end, vis[30], mark[105];
- char sb[105], v;
- void Init() {
- sum = 0;
- judge = 1;
- memset(vis, 0, sizeof(vis));
- memset(mark, 0, sizeof(mark));
- gets(sb);
- len = strlen(sb);
- end = len - 1;
- }
- void Judge() {//判断能不能组成回文
- int bo = 0;
- for (int i = 0; i < len; i ++)
- vis[sb[i] - 'a'] ++;
- for (int i = 0; i < 26; i ++) {
- if (vis[i] % 2) {
- bo ++;
- if (bo == 2) {
- judge = 0;
- break;
- }
- v = i + 'a';
- }
- }
- }
- void solve() {//变换
- for (int i = 0; i < len / 2; i ++) {
- int j;
- for (j = end; j >= i + 1; j --)
- if (sb[j] == sb[i]) {
- mark[i] = 1;
- sum += end - j;
- for (int k = j; k < end; k ++)
- sb[k] = sb[k + 1];
- end --;
- break;
- }
- }
- if (len % 2) {//奇数情况
- for (int i = 0; i < len; i ++)
- if (sb[i] == v && mark[i] == 0) {
- sum += len / 2 - i;
- break;
- }
- }
- if (judge)
- printf("%d\n", sum);
- else
- printf("Impossible\n");
- }
- int main() {
- scanf("%d%*c", &t);
- while (t --) {
- Init();
- Judge();
- solve();
- }
- return 0;
- }
UVA 10716 Evil Straw Warts Live(贪心)的更多相关文章
- uva 10716 Evil Straw Warts Live(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- UVa 10716 - Evil Straw Warts Live
题目大意:给一个字符串,判断是否能通过交换字母构成回文,如果能,计算所需的最小交换次数. 如果字符串中出现奇数次的字母的个数>1,则不能构成回文.然后...就没思路了...看网上说用贪心的思想先 ...
- 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 ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
- uva 1153 顾客是上帝(贪心)
uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...
- UVA 538 - Balancing Bank Accounts(贪心)
UVA 538 - Balancing Bank Accounts 题目链接 题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案 思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最 ...
- UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- jquery处理textarea中的手动换行
textarea的手动换行会产生换行标志,但这个标志存在却看不到,存入数据库中后读出来显示在页面上却不会换行,如何处理呢? 网上众说纷纭,经过测试用 textarea的内容.replace(/\n/g ...
- PHP中的ob_start() 的使用
1.在PHP编程中, 我们经常会遇到一些直接产生输出的函数, 如passthru(),readfile(), var_dump() 等. 但有时我们想把这些函数的输出导入到文件中,或者先经过处理再输出 ...
- js 函数命名
1 函数命名可以使用匿名: var f=function(x){return x*2;} 2 可以使用变量: function double(x){return x*2;} 二者区别:后者会绑定到与其 ...
- 数据库性能优化一:SQL索引一步到位
SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开他的神秘面纱. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引 ...
- Ubuntu 使用top/free查看内存占用大的原因
Ubuntu 使用top/free查看内存占用大的原因 linux/ubuntu下free/top查看内存占用大的原因 使用free/top查看内存占用的时候,吓了一大跳,机器4GB的内存,显 ...
- struts2自定义类型转换器
首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...
- ios专题 -block用法
what is block Blocks are a language-level feature added to C, Objective-C and C++, which allow you t ...
- Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)
1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...
- 01_JavaMail_04_带附件邮件的发送
[工程截图] [代码实例] package com.Higgin.mail.demo; import java.io.File; import java.util.Properties; import ...
- SCNU省选校赛第二场B题题解
今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...