您的位置:首页 > 理论基础

Is It Symmetric 浙大计算机研究生保研复试上机考试-2011年

2011-07-08 09:31 295 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3793

就是加强版的回文串

3793Is It Symmetric浙大计算机研究生保研复试上机考试-2011年 (57/136)41.91%
/*

Description:

先寻找位置C,

设C左边的字符个数大于C右边的字符个数, 即为L>R;

则寻找的C满足C左边的R个字符与C右边的R个字符对称,然后判断C左边剩下的字符是否

回文串,若是,则以C为中心,可以构成对称的串。

若C是R>L,可以类似;

*/

#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<string>

using namespace std;

int center = 0;

bool IsHuiWen(string str, int b, int s)

{

int i = b, j = s;

while(i <= j){

if(str[i] != str[j])

return false;

++i;

--j;

}

return true;

}

bool Judge(string str)

{

int len = str.length();

int i, j, c;

for(c = 0; c < len; ++c){

i = c - 1; j = c + 1;

center = c;

while(i >= 0 && j < len){

if(str[i] != str[j])

break;

--i;

++j;

}

if(i >= 0 && j < len)

continue; /*此时C不能当做中心,继续穷举下一个C位置*/

else if(i < 0 && j >= len)

return true;

else if(i >= 0 && IsHuiWen(str, 0, i))

return true;

else if(j < len && IsHuiWen(str, j, len-1))

return true;

}

return false;

}

int main()

{

string str;

while(cin>>str && str != "#"){

bool ys = Judge(str);

if(ys)

cout<<"YES "<<center<<endl;

else

cout<<"NO"<<endl;

}

////system("pause");

return 0;

}

本文出自 “东方快翔” 博客,请务必保留此出处http://hustluy.blog.51cto.com/1792080/606033
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: