枚举这个结尾的字串最大的回文长度,不行的话减一到没有,这是枚举的方法。这个字串长度最多是上一次的长度加上二个元素。
#include <bits/stdc++.h>
using namespace std;
int n,i,x,ans,r[100010];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin>>n;
r[1]=1;
for(i=2;i<=n;i++){
r[i]=min(i,r[i-1]+2);
while(r[i]){
cout<<"? "<<i-r[i]+1<<" "<<i<<endl;
cin>>x;
if(x==1) break;
r[i]--;
}
ans=max(ans,r[i]);
}
cout<<"! "<<ans<<endl;
}
喜提 38 分。
然后我想到了一种奇怪的优化:我们用类似剪枝的想法,如果 那么对答案的贡献是没有的,如果 则更没有贡献,所以我们只需要执行到 即可。
然后显而易见了。
#include <bits/stdc++.h>
using namespace std;
int n,i,x,ans,r[100010];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin>>n;
r[1]=1;
for(i=2;i<=n;i++){
r[i]=min(i,r[i-1]+2);
while(r[i]>ans){
cout<<"? "<<i-r[i]+1<<" "<<i<<endl;
cin>>x;
if(x==1) break;
r[i]--;
}
ans=max(ans,r[i]);
}
cout<<"! "<<ans<<endl;
}