hdoj 5311 Hidden String 【KMP + 暴力】

Hidden String

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

Total Submission(s): 1434    Accepted Submission(s): 514

Problem Description

Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a string s of
length n.
He wants to find three nonoverlapping substrings s[l1..r1], s[l2..r2], s[l3..r3] that:

1. 1≤l1≤r1<l2≤r2<l3≤r3≤n

2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is
"anniversary".

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤100),
indicating the number of test cases. For each test case:

There‘s a line containing a string s (1≤|s|≤100) consisting
of lowercase English letters.

Output

For each test case, output "YES" (without the quotes) if Soda can find such thress substrings, otherwise output "NO" (without the quotes).

Sample Input

2
annivddfdersewwefary
nniversarya

Sample Output

YES
NO

题意:给你一个字符串,让你从中找出s1[l1, r1]、s2[l2, r2]、s3[l3,r3]三段子串组成字符串anniversary,且三段子串满足l1 <= r1 <= l2 <= r2 <= l3 <= r3。问你能不能找到这样的三段子串。

思路:对字符串anniversary,每次枚举字符串中两个分割点i
和 j,分别构成三个相应的子串,查找在文本串中是否存在。

注意查找成功后,要重新构建文本串。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[10];
char ss[] = {"anniversary"};
char str[210], str1[210], str2[210];
void getfail(char *P)
{
    int l = strlen(P);
    f[0] = f[1] = 0;
    for(int i = 1; i < l; i++)
    {
        int j = f[i];
        while(j && P[i] != P[j])
            j = f[j];
        f[i+1] = P[i] == P[j] ? j + 1 : 0;
    }
}
int Find(char *T, char *P)//在字符串T中 查找字符串P
{
    int l1 = strlen(T);
    int l2 = strlen(P);
    int j = 0;
    for(int i = 0; i < l1; i++)
    {
        while(j && T[i] != P[j])
            j = f[j];
        if(T[i] == P[j])
            j++;
        if(j >= l2)
            return i+1;//返回最后匹配位置
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", str);
        bool flag = false;
        char s[10];
        int p, pos;
        for(int i = 1; i <= 9; i++)//第一分割点
        {
            if(flag)
                break;
            for(int j = i; j <= 9; j++)//第二分割点
            {
                //截取第一个字符串
                p = 0;
                for(int k = 0; k < i; k++)
                    s[p++] = ss[k];
                s[p] = '\0';
                getfail(s);//求失配函数
                if(Find(str, s) != -1)//查找
                {
                    pos = Find(str, s);
                    p = 0;
                    int len = strlen(str);
                    for(int k = pos; k < len; k++)//重构文本串
                        str1[p++] = str[k];
                    str1[p] = '\0';
                }
                else
                    continue;
                //截取第二个字符串
                p = 0;
                for(int k = i; k <= j; k++)
                    s[p++] = ss[k];
                s[p] = '\0';
                getfail(s);
                if(Find(str1, s) != -1)
                {
                    pos = Find(str1, s);
                    p = 0;
                    int len = strlen(str1);
                    for(int k = pos; k < len; k++)//重构文本串
                        str2[p++] = str1[k];
                    str2[p] = '\0';
                }
                else
                    continue;
                //截取第三个
                p = 0;
                for(int k = j+1; k < 11; k++)
                    s[p++] = ss[k];
                s[p] = '\0';
                getfail(s);
                if(Find(str2, s) != -1)
                {
                    flag = true;//第三个成功就 ok了
                    break;
                }
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 870    Accepted Submission(s): 384

Problem Description

A sequence b1,b2,?,bn are
called (d1,d2)-arithmetic
sequence if and only if there exist i(1≤i≤n) such
that for every j(1≤j<i),bj+1=bj+d1and
for every j(i≤j<n),bj+1=bj+d2.

Teacher Mai has a sequence a1,a2,?,an.
He wants to know how many intervals [l,r](1≤l≤r≤n) there
are that al,al+1,?,ar are (d1,d2)-arithmetic
sequence.

Input

There are multiple test cases.

For each test case, the first line contains three numbers n,d1,d2(1≤n≤105,|d1|,|d2|≤1000),
the next line contains n integers a1,a2,?,an(|ai|≤109).

Output

For each test case, print the answer.

Sample Input

5 2 -2
0 2 0 -2 0
5 2 3
2 3 3 3 3

Sample Output

12
5

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 19:06:58

hdoj 5311 Hidden String 【KMP + 暴力】的相关文章

HDU 5311 Hidden String (暴力)

题意:今天是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&q

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)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1499    Accepted Submission(s): 534 Problem Description Today is the 1st anniversar

HDU 5311 Hidden String (优美的暴力)

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 52    Accepted Submission(s): 25 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a

(BC 一周年)hdu 5311 Hidden String

Hidden String Accepts: 437 Submissions: 2174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为nn的字符串ss. 他想要知道能否找到ss的三个互不相交的子串s[l_1..r_1]s[l?1??..r?1??], s[l_2..r_2]s[l?2??..r?2

hdu 5311 Hidden String dp o(n)算法 深搜

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 857    Accepted Submission(s): 322 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

HDU 5310 Hidden String(暴力枚举)

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 429    Accepted Submission(s): 161 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

Hdu 5311 Hidden String

一道查找字符串的题,要求在给出的字符串中找出三段字符串a,b,c,其中a,b,c三个字符串有先后关系,且不能有交集,即原字符串中的一个字母不能被用两次. 这三个字符串拼成“anniversary”. 先后A了两次,第一次用<cstring>头文件中的strncpy和strstr函数.我再昨天也写了一篇随笔,简单介绍了这两个函数的使用方法,可移步至此. 为了确保先后关系,比如“versaryanni”,虽然可以找到“versary”和“anni”但顺序不对,所以不能,必须依次寻找,且第二次和第三

hdu 5311 Hidden String(dfs)(好题)

题意:匹配串为"anniversary",对于每个给定串,为是否存在不多于3段连续子串能拼成匹配串:(n<=100) 思路:两个串从首部开始搜,若匹配次数不大于3则成功,否则不成功: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m,len,flag; char ch[15]="anniversary";

HDU 2594 Simpsons’ Hidden Talents KMP题解

KMP的应用.直接使用s1产生next 数组,然后在s2中搜索s1,那么记录最后一个搜索到的数值,就是s1的前缀在s2中的最长后缀了. 本题应该不能直接调用strstr了吧. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <li