首页 > 代码库 > zju 2744 回文字符 hdu 1544
zju 2744 回文字符 hdu 1544
#include<iostream>#include<algorithm>using namespace std;int main(){ string s; while(cin>>s) { int i,begin,end,total = s.size(),l = s.size(); for(i = 0 ; i < s.size(); ++i) { begin = i - 1; end = i+1;//当字符串长度为奇数时 while(s[begin]==s[end]&&begin >= 0&&end < l) { total++;begin--;end++; } begin = i;end = i+1;//当字符串长度为偶数时 while(s[begin]==s[end]&&begin >= 0&&end < l) { total++;begin--;end++; } } cout<<total<<endl; } return 0; }
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int i,begin,end,total = s.size(),l = s.size();
for(i = 0 ; i < s.size(); ++i)
{
begin = i - 1; end = i+1;//当字符串长度为奇数时
while(s[begin]==s[end]&&begin >= 0&&end < l)
{
total++;begin--;end++;
}
begin = i;end = i+1;//当字符串长度为偶数时
while(s[begin]==s[end]&&begin >= 0&&end < l)
{
total++;begin--;end++;
}
}
cout<<total<<endl;
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。