题意:今天是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".

特别注意:式子中红色符号!!!

思路:其实就是要在一个串中找可能存在的3个连续子串来构成这个"anniversary",穷举第一个串的大小,再穷举第2个串的大小,剩下的由第3个串来搞定。每个串必须大于等于1的大小才行,而且不能重叠到。

如果用的是string自带的find,还需要特别注意的是OJ上string::npos可能并不是你机器上定义的那样,可能是-1,可能是unsigned的无穷大!总之最好避开这些东西~ 就是这里wa到怕了,因为BC和HDU上的并不一样,所以我在BC上交就过了,而HDU没过。

BC上交的:

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=; string str;
string tmp="anniversary"; int cal(int len)
{
if(len<tmp.size()) return false;//不可能
if(str.find( tmp )!= string::npos) return true;
if(len==tmp.size()) //相同串
{
if(str.find( tmp )!= string::npos) return true;
else return false;
} for(int i=; i<; i++)
{
unsigned int s1=str.find(tmp.substr(, i));
if( s1+tmp.size()>=len || s1==string::npos ) continue; for(int j=; j+i<; j++)
{
unsigned int s2=str.find( tmp.substr(i, j), s1+i );
if( s2==string::npos ) continue;
int k=-j-i;
if( str.find( tmp.substr(i+j, k), s2+j )!=string::npos)
{
return true;
} }
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
int n, m, t, p, q; cin>>t;
while(t--)
{
cin>>str;
bool tmp=cal(str.size());
if(tmp) puts("YES");
else puts("NO");
}
return ;
}

AC代码

HDU 上的:

#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=; string str;
string tmp="anniversary";
int cal(int len)
{
for(int i=; i+<=tmp.size(); i++)
{
int s1=str.find(tmp.substr(, i), );
if( s1< ) continue; for(int j=; j+i+<=tmp.size(); j++)
{
int s2=str.find( tmp.substr(i, j), s1+i );
if( s2< ) continue; int k=tmp.size()-j-i;
if(k<) continue;
if( str.find( tmp.substr(i+j, k), s2+j )< )
return true;
}
}
return false;
}
int main()
{
freopen("input.txt", "r", stdin);
int n, m, t, p, q;
cin>>t;
while(t--)
{
cin>>str;
if( cal( str.size() ) ) printf("YES\n");
else printf("NO\n");
}
return ;
}

AC代码

 

HDU 5311 Hidden String (暴力)的更多相关文章

  1. hdu 5311 Hidden String

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Description Today is the 1st anniv ...

  2. hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)  ...

  3. HDU 5311 Hidden String (优美的暴力)

    Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  4. hdu 5311 Hidden String 字符串

    BC一周年的题.这道题做比赛的时候A了小数据,终于评判的时候还是挂了,看来还是不认真思考的问题啊.交的时候 都没有信心过肯定是不行的.认真思考.敲一发,有信心过才是真正的acmer.赛后认真想了想,发 ...

  5. hdu 5311 Hidden String(find,substr)

    Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...

  6. hdoj 5311 Hidden String(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5311 思路分析:该问题要求在字符串中是否存在三个不相交的子串s[l1..r1], s[l2..r2], ...

  7. HDU 5311:Hidden String

    Hidden String  Accepts: 437  Submissions: 2174  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit ...

  8. BestCoder 1st Anniversary ($) 1002.Hidden String

    Hidden String Accepts: 437 Submissions: 2174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 26 ...

  9. BestCoder 1st Anniversary B.Hidden String DFS

    B. Hidden String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/co ...

随机推荐

  1. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  2. C# mongodb [下]

    概述 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(d ...

  3. [转载]c# OpenFileDialog

    string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog();            ...

  4. Struts2 常用的常量配置

    在struts2-core-2.1.8.1.jar的org.apache.struts2包下面的default.properties资源文件里可以查到常用的常量配置,这些不用刻意的记住:忘记的时候可以 ...

  5. java 追加写入代码一例

    最近最项目参数化的时候用到,场景是这样的,需要测试A和B两个接口,其中B接口传入的参数必须是传递给A接口过的,所以整理一个思路就是: 1. 正常调用A接口,但是将传递给A接口的参数保存到文本里,此处要 ...

  6. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  7. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  8. JAVA:23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  9. static int和static final int的区别

    1.static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是: 对于静态变量在内 ...

  10. 测试Tomcat