Uva--10716(贪心)

2014-07-25 16:31:38

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,2,3,4,5,...,n-1,n,当前逼近到第二层,考虑为 第2个 和 第n-1个 字符匹配回文字符需要的交换次数,取较小的。
 1 /*************************************************************************
 2     > File Name: i.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Fri 25 Jul 2014 03:23:33 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 void Mov(char *s,int p1,int p2){
17     char tmp;
18     if(p2 > p1)
19         for(int i = p1 + 1; i <= p2; ++i){
20             tmp = s[i - 1];
21             s[i - 1] = s[i];
22             s[i] = tmp;
23         }
24     else
25         for(int i = p1 - 1; i >= p2; --i){
26             tmp = s[i + 1];
27             s[i + 1] = s[i];
28             s[i] = tmp;
29         }
30 }
31 
32 void Rev(char *s,int cnt){
33     char tmp;
34     int k = cnt / 2;
35     for(int i = 0; i < k; ++i){
36         tmp = s[i];
37         s[i] = s[cnt - 1 - i];
38         s[cnt - 1 - i] = tmp;
39     }
40 }
41 
42 int main(){
43     int Case,len,sum;
44     char s[105];
45     scanf("%d",&Case);
46     while(Case--){
47         scanf("%s",s);
48         len = strlen(s);
49         sum = 0;
50         int flag = 1;
51         for(int i = 0; i < len / 2; ++i){
52             int pos1 = -1;
53             int pos2 = -1;
54             //stan : i
55             for(int j = len - 1 - i; j > i; --j)
56                 if(s[i] == s[j]){
57                     pos1 = j;
58                     break;
59                 }
60             //stan : len - 1 - i
61             for(int j = i; j < len - 1 - i; ++j){
62                 if(s[len - 1 - i] == s[j]){
63                     pos2 = j;
64                     break;
65                 }
66             }
67             if(pos1 == -1 && pos2 == -1){
68                 flag = 0;
69                 break;
70             }
71             if(pos1 != -1 && (pos2 == -1 || len - 1 - i - pos1 <= pos2 - i)){
72                 Mov(s,pos1,len - 1 - i);
73                 sum += len - 1 - i - pos1;
74             }
75             else{
76                 Mov(s,pos2,i);
77                 sum += pos2 - i;
78             }
79         }
80         if(flag)
81             printf("%d\n",sum);
82         else
83             printf("Impossible\n");
84     }
85     return 0;
86 }
posted @ 2014-07-25 16:36  Naturain  阅读(168)  评论(0编辑  收藏  举报