zoukankan      html  css  js  c++  java
  • POJ 1854 贪心(分治)

    Evil Straw Warts Live
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 1144   Accepted: 330

    Description

    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" 

    Input

    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

    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
    

    Sample Output

    3
    Impossible
    2
    

    Source

    题意:给你一个字符串,求最少交换相邻字符多少次能将它变为回文串。
    思路:先处理两边的,然后删掉,递归操作。
    代码:
     1 #include"cstdio"
     2 #include"map"
     3 #include"set"
     4 #include"cmath"
     5 #include"queue"
     6 #include"vector"
     7 #include"string"
     8 #include"cstring"
     9 #include"ctime"
    10 #include"iostream"
    11 #include"cstdlib"
    12 #include"algorithm"
    13 #define db double
    14 #define ll long long
    15 #define vec vector<ll>
    16 #define mt  vector<vec>
    17 #define ci(x) scanf("%d",&x)
    18 #define cd(x) scanf("%lf",&x)
    19 #define cl(x) scanf("%lld",&x)
    20 #define pi(x) printf("%d
    ",x)
    21 #define pd(x) printf("%f
    ",x)
    22 #define pl(x) printf("%lld
    ",x)
    23 //#define rep(i, x, y) for(int i=x;i<=y;i++)
    24 #define rep(i, n) for(int i=0;i<n;i++)
    25 const int N   = 1e6 + 5;
    26 const int mod = 1e9 + 7;
    27 const int MOD = mod - 1;
    28 const int inf = 0x3f3f3f3f;
    29 const db  PI  = acos(-1.0);
    30 const db  eps = 1e-10;
    31 using namespace std;
    32 int ans;
    33 int c[26];
    34 char s[8005];
    35 void swap(char &e,char &f){
    36     char ch;
    37     ch=e,e=f,f=ch;
    38 }
    39 int dfs(int l,int r)
    40 {
    41     if(l>=r) return ans;
    42     int lp=N,rp=-N;
    43     for(int i=l;i<r;i++){//移动字符到左边与右边配对
    44         if(s[i]==s[r]){
    45             lp=i;
    46             break;
    47         }
    48     }
    49     for(int i=r;i>l;i--){//移动字符到右边与左边配对
    50         if(s[i]==s[l]){
    51             rp=i;
    52             break;
    53         }
    54     }
    55     if(lp-l<=r-rp){
    56         ans+=lp-l;
    57         for(int i=lp;i>l;i--) swap(s[i],s[i-1]);
    58     }
    59     else{
    60         ans+=r-rp;
    61         for(int i=rp;i<r;i++) swap(s[i],s[i+1]);
    62     }
    63     return dfs(l+1,r-1);
    64 }
    65 int main()
    66 {
    67     int n;
    68     ci(n);
    69     while(n--)
    70     {
    71         cin>>s;
    72         memset(c,0, sizeof(c));
    73         int len=strlen(s);
    74         for(int i=0;s[i];i++) c[s[i]-'a']++;
    75         int ok=0;
    76         for(int i=0;i<26;i++) if(c[i]&1) ok++;
    77         ans=0;
    78         if(ok>1) puts("Impossible");
    79         else pi(dfs(0,len-1));
    80     }
    81 }
  • 相关阅读:
    NLPIR大数据语义系统文本数据分析挖掘平台
    NLPIR:中文语义挖掘是自然语言处理的关键
    NLPIR-JZSearch智能搜索深层挖掘大数据资源
    NLPIR智能Protege知识图谱实现多场景应用
    DotNet加密方式解析--散列加密
    SSO单点登录PHP简单版
    xss框架基础框架实现
    Kafka Offset Storage
    C#如何使用ES
    模拟Vue之数据驱动
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8995175.html
Copyright © 2011-2022 走看看