zoukankan      html  css  js  c++  java
  • 暴力程序之回文子串

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <stack>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <map>
    #include <bitset>
    using namespace std ;
    #define zero {0}
    #define INF 2000000000
    #define eps 1e-6
    typedef long long LL;
    int Max=0;
    char a[1000];
    int n;
    char it[100];
    int  f(string s)
    {
        //cout<<s<<endl;
        int len=s.size();
        int i,j,k;
        int maxx=1;
        string ss="";
        string re;
        for(i=0;i<len;i++)
        {
            ss+=s[i];
            for(j=i+1;j<len;j++)
            {
                ss+=s[j];
            //    cout<<ss<<endl;
                re=ss;
                reverse(re.begin(),re.end());
                if(ss==re)
                {
                    maxx=max(int(ss.size()),maxx);
                }
            }
            ss="";
        }
        return maxx;
    }
    void dfs(int i)
    {
         if (i!=n)
         {
              a[i]='a';
              dfs(i+1);
              a[i]='b';
              dfs(i+1);
         }
         else
         {
             if(f(string(a))<Max)
             {
                 Max=f(string(a));
                 strcpy(it,a);
                 //printf("%s-------%d
    ",a,Max);
             }
         }
    }
    int main()
    {
        #ifdef DeBUG
            freopen("C:\Users\Sky\Desktop\1.in","r",stdin);
        //freopen("C:\Users\Sky\Desktop\打表.txt","w",stdout);//PLEASE DELETE IT!!!!!!!!!!!!!!!!!!!!!!!!
        #endif
        int i,j,k;
        string s;
        for(i=1;i<=20;i++)
        {
            Max=INF;
            memset(a,0,sizeof(a));
            n=i;
            printf("*****************长度%d
    ",i);
            dfs(0);
            printf("%s
    ",it);
        }
        
        return 0;
    }
    View Code

    由a,b组成的包含最短的(最长回文子串)的序列,写这个暴力是解决一道题的关键hdu4731

  • 相关阅读:
    execvp
    Linux系统调用getrlimit()与setrlimit()函数详解
    Va_start及Vsprintf应用
    waitpid()
    sdut 2408 Pick apples 夜
    poj 1273 Drainage Ditches 夜
    poj 1408 Fishnet 夜
    poj 1113 Wall 夜
    poj 1584 A Round Peg in a Ground Hole 夜
    poj 3007 Organize Your Train part II 夜
  • 原文地址:https://www.cnblogs.com/Skyxj/p/3321644.html
Copyright © 2011-2022 走看看