首页 > 代码库 > 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;   }
View Code

#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;  
}