您的位置:首页 > 其它

hoj 5311 字符串,dfs

2015-11-28 14:15 295 查看
题意好懂的很。。。

题意:

给一个串,然后叫你找三节串,组合起来形成一个特定的串;

理解:

这题是Bestcoder上的,当时比赛并没有做出来;

前几天想了下,发现可以做;

我理解为深搜dfs,找3个子串组成特定的串;

然后搜3层;

判断组合在一起是否是特定的串;

当然,你可能只找到一串或两串就形成特定的串了;

这两种情况是可以特判的。

代码如下:

#include <cstdio>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>

using namespace std;

#define P pair<int, int>
#define x first
#define y second
#define ll long long

string s = "anniversary";
string str[4];
bool flag = false;

void dfs(int j, string st, int pos, int l)
{
if (pos == 3 && str[0] != "" && str[1] != "" && str[2] != "")
{
string ans = "";
for (int i = 0; i < 3; ++i) ans += str[i];
if (ans == s) flag = true;
return ;
}

int len = 0;
for (int i = j; i < st.length() + 1; ++i)
{
if (st[i] == s[l + len] && i < st.length() && l + len < s.length())
{
str[pos] += st[i];
++len;
}
else if (str[pos] != "")
{
dfs(i + 1, st, pos + 1, l + len);
if (flag) return ;
str[pos] = "";
len = 0;
}
}
if (str[0] == s || s == str[0] + str[1]) flag = true;   //只找到一串或两串就形成了特定的串
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
flag = false;
for (int i = 0; i < 3; ++i) str[i] = "";
string st;
cin >> st;
dfs(0, st, 0, 0);
cout << (flag ? "YES" : "NO") << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hoj dfs string