码迷,mamicode.com
首页 > 其他好文 > 详细

ZeptoLab Code Rush 2015 A. King of Thieves

时间:2015-04-05 13:25:20      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:暴力搜索

1.题目描述:点击打开链接

2.解题思路:本题要求在一个串中找出5个等距离的‘*’,如果有,输出yes,否则输出no。思路很清晰,首先枚举步长len,然后枚举起点i,判断是否能够连续不间断地跳跃5次且均为‘*’即可。本题还可以加速寻找,先寻找是否存在‘*****’,如果有,直接输出yes。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

string s;
int main()
{
	//freopen("t.txt", "r", stdin);
	int n;
	while (~scanf("%d", &n))
	{
		cin >> s;
		int i;
		for (i = 0; i < n; i++)
		if (s[i] == '*')//找到第一个‘*’的位置
			break;
		int cnt, ok = 0;
		if (strstr(s.c_str(), "*****") != NULL)ok = 1;//加速措施
		else
		{
			for (int len = 1; len < n; len++)//枚举步长
			{
				int cnt, k;
				for (int j = i; j < n - 4 * len; j++)//枚举起点
				{
					cnt = 0, k = 0;
					while (k < 5)
					{
						if (s[j + k*len] == '*')cnt++;
						else break;
						k++;
					}
					if (cnt == 5){ ok = 1; break; }
				}
			}
		}
		if (ok)puts("yes");
		else puts("no");
	}
	return 0;
}


ZeptoLab Code Rush 2015 A. King of Thieves

标签:暴力搜索

原文地址:http://blog.csdn.net/u014800748/article/details/44886759

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!