鏈接:https://ac.nowcoder.com/acm/contest/330/I
來源:牛客網
Applese 的迴文串
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
自從 Applese 學會了字符串之後,精通各種字符串算法,比如……判斷一個字符串是不是迴文串。
這樣的題目未免讓它覺得太無聊,於是它想到了一個新的問題。
如何判斷一個字符串在任意位置(包括最前面和最後面)插入一個字符後能不能構成一個迴文串?
輸入描述:
僅一行,爲一個由字母和數字組成的字符串 s。
輸出描述:
如果在插入一個字符之後可以構成迴文串,則輸出"Yes", 否則輸出"No"。
示例1
輸入
applese
輸出
No
示例2
輸入
java
輸出
Yes
備註:
|s|≤10^5
思路:
代碼:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
char s[maxn];
bool dfs(int l,int r,int n)
{
if(l>=r) return true;
if(s[l]==s[r]) return dfs(l+1,r-1,n);//表示是迴文串,左前進,右後退 ,本來傳的是1不是n
if(!n) return false; //但是一隻wa,傳n就對了
else return dfs(l+1,r,0)||dfs(l,r-1,0);
}/*只容許錯一個,若再錯則會遇到第三個if,返回假*/
int main()
{
scanf("%s",s);
if(dfs(0,strlen(s)-1,1)) printf("Yes\n");
else printf("No\n");
}