首页 > ACM题库 > HDU-杭电 > HDU 3793-Is It Symmetric-枚举-[解题报告]HOJ
2015
04-13

HDU 3793-Is It Symmetric-枚举-[解题报告]HOJ

Is It Symmetric

问题描述 :

It is easy to see that a string of digits like 1234321 is symmetric with 4 being the central digit. However it is less obvious if we consider the string as a circular one and shift it to the left as 2343211. Your task is to write a program to check if a circular string is symmetric.

输入:

Your program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains the string. Each string contains less than 100 digits.
The input is finished by a "#".

输出:

Your program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains the string. Each string contains less than 100 digits.
The input is finished by a "#".

样例输入:

2112343
798
#

样例输出:

YES 5
NO

水题。。判断一个串是否是以一个数为中心,旁边一次相等,对称。

枚举,以每个数为中心判断是否符合要求。。

#include<iostream>
using namespace std;
int main()
{
	char s[105];
	int i,j,n,l,r,flag;
	while(cin>>s)
	{
		if(!strcmp(s,"#"))return 0;
		n=strlen(s);
		for(i=0;i<n;i++)
		{
			flag=0;
			l=r=i;
			while(s[l]==s[r])
			{
				l=(l-1+n)%n;
		     	r=(r+1)%n;
				if(l==r)
				{
					flag=1;
					break;
				}
			}
			if(flag)break;

		}
		if(flag)cout<<"YES "<<i<<endl;
		else
			cout<<"NO\n";

	}
return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/yylxid/article/details/8968166


  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  3. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了