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