The number of Live replies in the UVA 10716 edevil Straw Warts is greedy.

Source: Internet
Author: User

A string is given. Each time two adjacent characters are exchanged, the minimum number of times the string is exchanged. Find the two outermost letters each time. If the two letters are the same, indent them inward. If the two letters are different, find the same closest characters on both sides, exchange them, and indent them inward. After a long call, you must clarify the algorithm and try again later... Code:

 /*  *   Author:        illuz <iilluzen[at]gmail.com>  *   Blog:          http://blog.csdn.net/hcbbt  *   File:          uva10716.cpp  *   Lauguage:      C/C++  *   Create Date:   2013-09-03 23:03:20  *   Descripton:    UVA 10716 Evil Straw Warts Live, greed  */  #include <cstdio>  #include <cstring>  #define rep(i, n) for (int i = 0; i < (n); i++)  #define swap(a, b) {int t = a; a = b; b = t;}  #define mc(a) memset(a, 0, sizeof(a))    const int MAXN = 110;  int n, len, app[26], cnt;  char s[MAXN];    void solve(char* a, int l) {      if (a[0] == a[l - 1]) return;      for (int i = 1; i < l - 1; i++)          if (a[i] == a[l - 1]) {              for (int j = i; j > 0; j--)                  swap(a[j], a[j - 1]);              cnt += i;              return;          }          else if (a[l - i - 1] == a[0]) {              for (int j = l - i - 1; j < l - 1; j++)                  swap(a[j], a[j + 1]);              cnt += i;              return;          }  }    int main() {      scanf("%d", &n);      while (n--) {          scanf("%s", s);          len = strlen(s);          mc(app);          rep(i, len) app[s[i] - 'a']++;          cnt = 0;          rep(i, 26) if (app[i] % 2) cnt++;          if (cnt > 1)              printf("Impossible\n");          else {              cnt = 0;              rep(i, len / 2)                   solve(s + i, len - 2 * i);              printf("%d\n", cnt);          }      }      return 0;  }  

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.