Evil Straw Warts Live

来源:互联网 发布:南京弘历软件 编辑:程序博客网 时间:2024/05/21 17:22

Evil Straw Warts Live

总时间限制: 2000ms 内存限制: 65536kB
描述
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 8000 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.
样例输入
3
mamad
asflkj
aabb
样例输出
3
Impossible
2

题意:给定一些字符串,要求出能否通过交换字母变换为回文。。如果可以输出最少变换次数。。
思路:贪心。。

1、先判断能不能变换为回文。。如果字符串中没有或只有1个字母是奇数。就可以组成。。

2、每次从第一个字母开始。从后往前找到一个相同字母。放到最后就是匹配了。。每次移动的次数为当前位置到最后的位置的距离。

要注意有单个字母为奇数的情况。。最后要把这个字母另外拿出来移到最中间。。一开始没考虑这个wa了- -

#include <stdio.h>#include <string.h>//http://bailian.openjudge.cn/exam/3658/5///并不会做,神奇的贪心算法 int t, len, sum, judge, end, vis[8005], mark[8005];char sb[8005], 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;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 租的房子没窗户怎么办 北京安河桥安河家园租房被骗怎么办 领完失业金后怎么办 北京公租房太小怎么办 申请公租房太小怎么办 房东电费收贵了怎么办 申请公租房工资超了怎么办 重庆公租房工资超了怎么办 公租房申请父母房子贷款怎么办 公产房父母去世办公证怎么办 动迁过程中承租人去世了怎么办 公租房的房间带阳台怎么办 公租房合同到期没有社保怎么办 租房合同没有到期违约了怎么办 租房户到期不搬怎么办 公租房摇号摇到了又怎么办 公租房被清退会怎么办 公租房摇不到号怎么办 公租房到期不搬怎么办 租房到期租客不搬怎么办 房产证面积与实际不符怎么办 社保晚交了1天怎么办 个人社保忘交了怎么办 个人社保晚交了怎么办 医保晚交了几天怎么办 辞职后转为灵活就业养老怎么办 公司名称变更提取不了公积金怎么办 五险合一软件已经减员怎么办 法人社保不在投标单位怎么办 换工作单位后社保怎么办 在北京孩子没有一老一小怎么办 深户小孩怎么办社保卡 社保卡没办下来去医院住院怎么办 老年社保卡丢了怎么办 外墙掉瓷砖伤车伤人怎么办 医保卡姓名弄错了怎么办 走工伤和走社保怎么办 公司在朝阳社保在海淀怎么办 公司没缴纳个税怎么办 报个税工资报少了怎么办 医院预约单丢了怎么办