题意
一个字符串是非回文的,当且仅当,他只由前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){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 1025;
char str[MAX_N];
int arr[MAX_N],ans[MAX_N],n,m;
bool dfs(int pos,bool flag)
{
if(pos==n+1) return true;
int sta;
if(flag) sta = 0;
else
{
sta = arr[pos];
if(pos==n) sta++;
}
for(;sta<m;++sta)
{
if((pos>1&&ans[pos-1]==sta) || (pos>2&&ans[pos-2]==sta)) continue;
if(sta>arr[pos]) flag = 1;
ans[pos] = sta;
if(dfs(pos+1,flag)) return true;
}
return false;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
scanf("%d%d",&n,&m);
scanf("%s",str+1);
for(int i = 1;i<=n;++i)
arr[i] = str[i] - 'a';
if(dfs(1,0))
{
for(int i = 1;i<=n;++i)
printf("%c",ans[i]+'a');
printf("\n");
}
else printf("NO\n");
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
过19组数据的代码
/*
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){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 1025;
const int maxn=1025;
char Ma[maxn*2+5];
int Mp[maxn*2+5];
void Manacher(char s[],int len)
{
int l=0;
Ma[l++]='$';
Ma[l++]='#';
for(int i=0; i<len; i++)
{
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=0;
int mx=0,id=0;
for(int i=0; i<l; i++)
{
Mp[i] = mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]])
Mp[i]++;
if(i+Mp[i]>mx)
{
mx = i +Mp[i];
id = i;
}
}
}
char s[MAX_N], str[MAX_N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",str);
string ans_ = "";
for(int i = 0;i<n;++i)
s[i] = str[i];
bool flag = false;
for(int i = n-1;i>=0;--i)
{
for(int j = str[i]-'a'+1;j<m;j++)
{
s[i] = 'a'+j;
Manacher(s,n);
int ans=0;
for(int i=0; i<2*n+2; i++)
ans = max(ans,Mp[i]-1);
if(ans==1)
{
flag = true;
ans_ = s;
break;
}
}
if(flag) break;
s[i] = str[i];
}
if(!flag) printf("NO\n");
else printf("%s\n",ans_.c_str());
return 0;
}