【51nod1523非回文】dfs

该博客讨论了51nod1523题目的解决方案,这是一个关于找到比给定非回文字符串字典序大且最小的非回文字符串的问题。作者首先尝试了贪心的方法,但发现该方法不正确。然后通过从第一个位置开始的深度优先搜索(DFS)策略,确保新加入的字符不会与左侧已有的字符形成回文,即避免偶数长度和奇数长度的回文子串。文章提到DFS的时间复杂度难以精确计算,并提供了最终通过19组数据的代码。
摘要由CSDN通过智能技术生成

51nod1523

题意

一个字符串是非回文的,当且仅当,他只由前p个小写字母构成,而且他不包含长度大于等于2的回文子串。

给出长度为n的非回文串s。请找出字典序比s大的,而且字典序要最小的长度为n的非回文。

一开始想了一个做法 贪心的去改一个字符 用manacher看是不是回文 改成功就一定行
时间复杂度 1000261000
25组数据过了19组
但是其实是fake的 因为你想
4 4
abcd
我这个做法会输出dbcd

但答案其实是 abda

我们考虑一下 怎么样才能非回文呢
我们从 1位置开始dfs
那么左边的所有串一定是非回文
新加入的串不和左边所有串形成回文
就是不能形成偶数回文 和奇数回文
也就是 arr[i]!=arr[i-1] 和 arr[i]!=arr[i-2]
dfs时间复杂度是 玄学~~

AC代码

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){
   return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){
   int ans=1;while(b){
   if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){
   return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值