Is It Symmetric 浙大计算机研究生保研复试上机考试-2011年
2011-07-08 09:31
295 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3793
就是加强版的回文串
/*
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
就是加强版的回文串
3793 | Is 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
相关文章推荐
- Is It Symmetric 浙大计算机研究生保研复试上机考试-2011年
- hdu 3794 浙大计算机研究生保研复试上机考试-2011年
- hdu 3792-浙大计算机研究生保研复试上机考试-2011年
- Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)
- hdu 3793 浙大计算机研究生保研复试上机考试-2011年
- Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)
- 浙大计算机研究生复试上机考试-2005年-A+B
- HDU-1231 最大连续子序列 (浙大计算机研究生复试上机考试-2005年 线性dp)
- hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题
- 浙大计算机研究生复试上机考试-2006年---《简单计算器》---栈的运用
- hdu1233浙大计算机研究生复试上机考试(2006)还是畅通工程
- 又一版 A+B 浙大计算机研究生复试上机考试-2008年
- 浙大计算机研究生复试上机考试-2005年-A+B
- 浙大计算机研究生复试上机考试(2010)——二叉搜索树(hdu3791)
- A+B HDU - 3787(浙大计算机研究生复试上机考试-2010年 )
- ZOJ问题 HDU - 3788(浙大计算机研究生复试上机考试-2010年 )
- 浙大计算机研究生复试上机考试-2010年
- 浙大计算机研究生复试上机考试-2006年_Prim_hdoj1233
- 浙大计算机研究生复试上机考试-2009年
- hdu 1881 毕业bg (01背包变形)浙大计算机研究生复试上机考试-2008年