Codeforces 505A Mr. Kitayuta's Gift

本文介绍了解决Codeforces505A问题的方法,包括两种主要思路:通过添加字符来构造回文串和通过删除字符来检查是否能形成回文串。详细解释了如何遍历字符串并进行相应操作,最终找到符合条件的解或输出“NA”。

Codeforces 505A Mr. Kitayuta's Gift


思路1: 字符串的长度一共是10,可以从'a'到'z' ,每一个位置添加后,尝试一遍

思路2: 从每个位置删除该字符,看剩余字符串是否构成字符串,如果构成字符串,则在对应位置添加该字符 

细节注意:i在0到len/2-1 和len/2+len-1处理方式不同

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	string temp,u,v; 
	string ans,t; 
	bool flag = false; 
	cin>>temp; 
	for ( int i=0; i<temp.length(); i++ ) {
		t = temp.substr(i,1); 
		u = temp.substr(0,i) + temp.substr(i+1,temp.length()-i-1);
		v = u; 
		reverse( v.begin(),v.end() ); 
		if ( u==v ) {
			if ( i<temp.length()/2 ) 
				ans = temp.substr(0,temp.length()-i)+t+temp.substr(temp.length()-i,i); 
			else ans = temp.substr(0,temp.length()-i-1)+t+temp.substr( temp.length()-i-1,i+1); 
			flag = true; 
		}
	}
	
	if ( flag ) cout<<ans<<endl; 
	else cout<<"NA"<<endl; 
	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值