• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FightingForWorldFinal
博客园    首页    新随笔    联系   管理    订阅  订阅

暴力程序之回文子串

#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\n",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\n",i);
        dfs(0);
        printf("%s\n",it);
    }
    
    return 0;
}
View Code

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

posted @ 2013-09-14 19:22  Sky-J  阅读(221)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3