BestCoder 1st Anniversary B.Hidden String DFS
2015-07-25 23:39
330 查看
B. Hidden String
Time Limit: 1 SecMemory Limit: 256 MB
题目连接
http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=610&pid=1002Description
今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下列条件: 1. 1≤l1≤r1<l2≤r2<l3≤r3≤n 2. s[l1..r1], s[l2..r2], s[l3..r3]依次连接之后得到字符串"anniversary".
Input
输入有多组数据. 第一行有一个整数T (1≤T≤100), 表示测试数据组数. 然后对于每组数据: 一行包含一个仅含小写字母的字符串s (1≤|s|≤100).
Output
对于每组数据, 如果Soda可以找到这样三个子串, 输出"YES", 否则输出"NO".
Sample Input
2 annivddfdersewwefary nniversarya
Sample Output
YES
NO
HINT
题意题解:
DFS
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef __int64 ll; using namespace std; typedef __int64 ll; const int inf = (int)1E9+10; inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //******************************* int T; char s[1111]; char s1[] = "anniversary"; bool dfs(int num,int k,int i) { if(num >= 3) return 0; while(s[k] != '\0') { if(s[k] == s1[i]) { k++; i++; if(dfs(num+1,k,i)) return true; if(s1[i] == '\0')return true; } else { break; } } for(; s[k] != '\0'; ++k) { if(s[k] == s1[i]) { if(dfs(num+1,k,i)) return true; } } return false; } int main() { cin>>T; while(T--) { scanf("%s",s); int flag=0; for(int i=0; s[i]!='\0'; ++i) { if(s[i]=='a') { flag=dfs(0,i,0); if(flag) break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 2178 猜数字(二分)
- 第三篇重点面向对象与封装
- Java面试题汇总
- 属性与字段的区别与联系
- Android中处理崩溃异常
- 先序序列和后序序列并不能唯一确定二叉树
- REDIS-命令
- HDOJ 5299 Circles Game 圆嵌套+树上SG
- HDU4282 A very hard mathematic problem 快速幂
- poj 3648 Wedding 【2-sat 经典建图 输出一组可行解 好题】 【tarjan求SCC + 缩点 + 拓扑排序 + 染色】
- 一步一步学习网页开发之问题记录一
- 学习Scala第一天 scala开发环境搭建和helloword解析
- leetcode 240 Search a 2D Matrix II
- ci高级用法篇之创建自己的类库
- redis(六)-主从配置
- 堆排序
- python生成一个字典的嵌套
- awk/grep正则匹配-找出以逗号分隔的数字列表
- const
- 最大熵模型