题意:今天是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. 仪表盘 hostmap 新玩法让运维工作越玩越 high

    Cloud Insight 第13次新品发布会现在开始,首先非常感谢大家前来看我们的新功能发布会,下面我先给大家介绍一下新功能,之后有什么问题大家尽管问

  2. .net中的认证(authentication)与授权(authorization)

    “认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...

  3. android Notification定义与应用

    首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...

  4. Opc

    http://www.tuicool.com/articles/nymUz2 http://blog.chinaunix.net/uid-20692368-id-3434001.html http:/ ...

  5. Jedis 操作

    http://www.cnblogs.com/liuling/p/2014-4-19-04.html

  6. Oracle 体系结构2 - 实例和数据库

    Oracle最最基本的概念: 实例和数据库 实例就是oracle进程和一块共享内存, 数据库就是静态的文件,如datafile, log file, redo logfile, control fil ...

  7. mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2)” 解决办法

    首先安装 homebrew 再 brew install mysql 之后连接 mysql 无论是登录还是修改初始密码都会报如下的错误 ERROR 2002 (HY000): Can not conn ...

  8. 李洪强漫谈iOS开发[C语言-039]-剪刀石头布

     李洪强漫谈iOS开发[C语言-039]-剪刀石头布

  9. 李洪强iOS开发之OC[015]#pragma mark的使用

    // //  main.m //  14 - #pragma mark的使用 // //  Created by vic fan on 16/7/10. //  Copyright © 2016年 李 ...

  10. Tomcat7.0 start Could not find the main class: org.apache.catalina.startup.Bootstrap.

    java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory at org.apache.catalina.startup.Bo ...