hoj 5311 字符串,dfs
2015-11-28 14:15
295 查看
题意好懂的很。。。
题意:
给一个串,然后叫你找三节串,组合起来形成一个特定的串;
理解:
这题是Bestcoder上的,当时比赛并没有做出来;
前几天想了下,发现可以做;
我理解为深搜dfs,找3个子串组成特定的串;
然后搜3层;
判断组合在一起是否是特定的串;
当然,你可能只找到一串或两串就形成特定的串了;
这两种情况是可以特判的。
代码如下:
题意:
给一个串,然后叫你找三节串,组合起来形成一个特定的串;
理解:
这题是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; }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
- JavaScript中的object转换成number或string规则介绍