HDU5311

时间:2023-03-09 15:22:53
HDU5311

题意:给一个指定的字符串a,要求分成三段,然后再给定另外一个字符串b,要求a中的三段能否在b中找到。

思路:枚举+模拟,首先枚举给定的字符串a,因为分成三段,所以一共有(1+9)*9/2种情况,对于分成后的三段p,q,r先查找p在b中匹配后的下标,然后减去b,结果是匹配字符的前一个下标,所以这个时候要加上匹配的长度,才能确定下个匹配从哪里开始,最后只要匹配成功即可退出。

#include<cstring>
#include<stdio.h>
#include<iostream>
using namespace std;
#define MAX 110
int t,flag;
char a[] = "anniversary", b[MAX];
bool solve(char s[])
{
for(int i = 0; i <= 8; i++)
{
for(int j = i + 1; j <= 9; j++)
{
strcpy(b, a);
b[i + 1] = 0;
flag = strstr(s, b) - s;
if(flag < 0) continue;
flag += i + 1;
strcpy(b, a + i + 1);
b[j - i] = 0;
flag = strstr(s + flag, b) - s;
if(flag < 0) continue;
flag += j - i;
strcpy(b, a + j + 1);
b[10- j] = 0;
flag = strstr(s + flag, b) - s;
if(flag < 0) continue;
return true;
}
}
return false;
}
int main()
{
cin>>t;
while(t--)
{
char s[MAX];
scanf("%s", s);
if(solve(s))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}