# HDU 5311 Hidden String

#include <bits\stdc++.h>
using namespace std;
string str, standard = "anniversary";
bool find(string str0, string str1, string str2) {//按顺序分别找到三个子串
int i, cnt = 0, N = str.size();
for (i = 0; i + str0.size() < N; ++i) {
if (str.substr(i, str0.size()) == str0) {
cnt++;
break;
}
}
for (; i + str1.size() < N; ++i) {
if (str.substr(i, str1.size()) == str1) {
cnt++;
break;
}
}
for (; i + str2.size() <= N; ++i) {
if (str.substr(i, str2.size()) == str2) {
cnt++;
break;
}
}
return cnt == 3;
}
int main() {
int T;
cin >> T;
for (int I = 1; I <= T; ++I) {
str.clear();
cin >> str;
int N = standard.size();
bool ok = false;
for (int i = 1; i < N && !ok; ++i) {
for (int j = 1; i + j < N; ++j) {
string str0 = standard.substr(0, i),
str1 = standard.substr(i, j),
str2 = standard.substr(i + j);
//这里先枚举出"anniversary"三个子串
if (find(str0, str1, str2)) {
ok = true;
break;
}
}
}
cout << (ok ? "YES" : "NO") << endl;
}
return 0;
}

Show me the code!

#include <iostream>
#include <vector>
using namespace std;
string str, standard = "anniversary";
int N = standard.size();
bool is_find(string t, int &pos) {
for (int a = pos; a + t.size() <= str.size(); ++a) {
if (t == str.substr(a, t.size())) {
pos = a + t.size();
return true;
}
}
return false;
}
int main() {
vector<string> vec;
for (int a = 1; a < N; ++a) {
for (int b = a + 1; b < N; ++b) {
string str0 = standard.substr(0, a),
str1 = standard.substr(a, b - a),
str2 = standard.substr(b);
vec.push_back(str0);
vec.push_back(str1);
vec.push_back(str2);
}
}
int T;
cin >> T;
for (int I = 1; I <= T; ++I) {
str.clear();
cin >> str;
bool ok = false;
for (int a = 0; a < vec.size(); a += 3) {
int b = 0;
if (is_find(vec[a + 0], b)
&& is_find(vec[a + 1], b)
&& is_find(vec[a + 2], b)) {
ok = true;
break;
}
}
cout << (ok ? "YES" : "NO") << endl;
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：HDU 5311 Hidden String 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)