【gmoj】 【暴力】 【字符串】 最长公共回文子序列

19 篇文章 0 订阅
14 篇文章 0 订阅

【gmoj】 【暴力】 【字符串】 最长公共回文子序列

题目

在这里插入图片描述


解题思路

m很小可以放肆的搞事情
用深搜从b串中找一个回文子序列
再判断是否在a中存在
保存最长的长度即可


代码

#include<iostream>
#include<cstdio>
using namespace std;
string s,x,y;
int lenx,leny,ans;
bool check(int l,int p)  //p标记的是回文串的奇偶
{
	  int w=1;
	  for (int i=1;i<=l;i++)
	  {
	      w=x.find(s[i],w)+1;
		  if (w>lenx||w==0) return 0;
	  }
	  if (p) l--;
	  for (int i=l;i>0;i--)
	  {
	      w=x.find(s[i],w)+1; 
		  if (w>lenx||w==0) return 0;
	  }
	  return 1;
}
void dfs(int pos,int id,int xz)  //回文串一半的长度,当前位置,限制(回文串会越来越靠近)
{
	 if (id>xz||pos>leny/2+1) return;
	 dfs(pos,id+1,xz);
	 while (y[xz]!=y[id]) xz--;  //是否能在b串中找到对应
	 s[pos]=y[id];
	 if (check(pos,xz==id)) 
	 { 
	 	if (xz==id) ans=max(pos*2-1,ans);
	 	   else ans=max(pos*2,ans);
	    dfs(pos+1,id+1,xz-1);
	 }
}
int main()
{
	freopen("lcps.in","r",stdin);
	freopen("lcps.out","w",stdout);
	cin>>x>>y;
	lenx=x.size();
	leny=y.size();
    dfs(1,0,leny-1);
	printf("%d\n",ans);
	return 0;
	fclose(stdin);
	fclose(stdout);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值