2016校招编程题<三>

      给定一个字符串.问是否可以通过添加一个字符将其变成回文串。


输入:一行由小写字母构成的字符串,长度不超过10。

输出:YES 或者NO


输入样例:testest

输出样例:YES


<span style="font-size:18px;">#include <iostream>
#include <string>

using namespace std;

bool fun(string str)
{
    if (str.length() == 0) return true;
    
    bool result = false;
    
    int low = 0;
    int high = str.length() - 1;
    
    while (low < high && str[low] == str[high]) {
        low++;
        high--;
    }
    
    if (high <= low) {
        result = true;
    } else
    {
        if (high == low + 1) {
            result = true;
        } else
        {
            int temp_low = low;
            int temp_high = high - 1;
            
            while (temp_low < temp_high && str[temp_low] == str[temp_high]) {
                temp_low++;
                temp_high--;
            }
            
            if (temp_low >= temp_high) {
                result = true;
            } else
            {
                temp_low = low + 1;
                temp_high = high;
                
                while (temp_low < temp_high && str[temp_low] == str[temp_high]) {
                    temp_low++;
                    temp_high--;
                }
                
                if (temp_low >= temp_high) {
                    result = true;
                } else
                {
                    result = false;
                }
            }
        }
    }
    
    return result;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    string str;
    
    cin >> str;
    
    bool result = fun(str);
    
    if (result) {
        cout << "Yes" << endl;
    } else
    {
        cout << "No" << endl;
    }
    
    return 0;
</span>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值