hiho一下 第五十八周

题目1 : Beautiful String

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.)

Here are some example of valid beautiful strings: "abc", "cde", "aabbcc", "aaabbbccc".

Here are some example of invalid beautiful strings: "abd", "cba", "aabbc", "zab".

Given a string of alphabets containing only lowercase alphabets (a-z), output "YES" if the string contains a beautiful sub-string, otherwise output "NO".

输入

The first line contains an integer number between 1 and 10, indicating how many test cases are followed.

For each test case: First line is the number of letters in the string; Second line is the string. String length is less than 10MB.

输出

For each test case, output a single line "YES"/"NO" to tell if the string contains a beautiful sub-string.

提示

Huge input. Slow IO method such as Scanner in Java may get TLE.

样例输入
4
3
abc
4
aaab
6
abccde
3
abb
样例输出
YES
NO
YES
NO

hiho一下第58周《Beautiful String》题目分析

12
7

题意分析

给定字符串S,判定S是否存在子串S’满足"aa…abb…bcc…c"的形式。其中abc为连续的三个字母,且a,b,c的数量相同。

原题目中数量相等的连续n(n>3)个字母也是可行的,而实际上当n>3时一定包含有n=3的情况。比如"abcd"就包含有"abc""bcd"两个合法子串。

算法分析

最基本的思路为对S的每一个子串进行判定是否满足要求。枚举子串的起点、终点以及检查是否合法。

假设S的长度为N,则时间复杂度为O(N^3)

For i = 0..N-1
    For j = 0..N-1
        check(S[i..j])
    End For
End For

这样的做法对于N稍大的数据来说就会超过时限。

进一步考虑,由于合法子串中相同的字母总是连续的,我们不妨用(c,n)来表示一串连续相同的字母,比如"aaa"表示(a,3)"bb"表示为(b,2)

我们将整个字符串S用(c,n)表示,得到{(c[1], n[1]),(c[2],n[2]),...,(c[t],n[t])}的序列。其中我们合法的子串也可以表示为{(a,n),(b,n),(c,n)}

则算法改变为在序列{(c[1], n[1]),(c[2],n[2]),...,(c[t],n[t])}中判定是否存在连续的3个元素满足c[i],c[i+1],c[i+2]连续且n[i] == n[i+1] == n[i+2]

预处理时间为O(N),得到的序列长度最大为N,所以整体的时间复杂度降低为O(N)

For i = 1 .. t-2
    If (c[i]+1 == c[i+1] and c[i+1]+1 == c[i+2]) and (n[i] == n[i+1] == n[i+2])
        Return True
    End If
End For

然而实际运行会发现,这个算法是不正确的。比如:"aaaabbccc",其对应的序列为{(a,4),(b,2),(c,3)},根据我们上面的算法并不能找到合法子串。但实际上存在合法子串"aabbcc"

很显然,问题出在我们对于n[i],n[i+1],n[i+2]的判定上。通过上面的反例我们可以发现,在子串中n[i],n[i+2]的值其实是可以变动的,唯一固定的是n[i+1]的值。当n[i]>n[i+1]时,我们只要删去前面的若干个字母,就能够使得n[i]==n[i+1]。同理对于n[i+2]>n[i+1]时,我们删去后面的字母。因此只要有n[i]>=n[i+1],n[i+2]>=n[i+1],就一定能够通过变换使得n[i] == n[i+1] == n[i+2]

改正后我们的算法代码为:

For i = 1 .. t-2
    If (c[i]+1 == c[i+1] and c[i+1]+1 == c[i+2]) and (n[i] >= n[i+1] and n[i+1] <= n[i+2])
        Return True
    End If
End For

结果分析

在实际的比赛中,该题目的通过率仅为26%。

但根据赛后的统计结果,大部分的选手都使用了朴素的算法通过了规模较小的数据点。在该题目上获取了10~60不等的分数。

其中比较有意思的是有一位选手仅仅判定连续3个字母是否连续,也获得了60的分数。

而分布在70~90分数段的程序,随机抽取了若干样本,发现大多数都是想到了正确算法的。而导致他们丢分的主要原因则是多组数据产生的初始化问题。


CODE:

#include <bits/stdc++.h>
using namespace std;

const int maxn=1024*1024*10+5;
char ch[maxn],c[maxn];
int num[maxn];

int main()
{
    int t; cin>>t;
    while(t--){
        int len;
        memset(ch,0,sizeof(ch));
        cin>>len>>ch;
        int cnt=0;
        memset(num,0,sizeof(num));
        for(int i=0;i<len;i++){
            if(cnt==0){
                cnt++;
                num[cnt-1]++;
                c[cnt-1]=ch[i];
            }
            else if(ch[i]!=c[cnt-1]){
                cnt++;
                num[cnt-1]++;
                c[cnt-1]=ch[i];
            }
            else
                num[cnt-1]++;
        }
//        for(int i=0;i<cnt;i++)
//            cout<<num[i];
        if(cnt<3){
            cout<<"NO"<<endl;
            continue;
        }
        bool flag=false;
        for(int i=1;i<cnt-1;i++){
            if(c[i-1]+1==c[i]&&c[i]+1==c[i+1]&&
               num[i-1]>=num[i]&&num[i+1]>=num[i]){
                flag=true;
                break;
            }
        }
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

CODE:(一开始一直40分)

#include <bits/stdc++.h>
using namespace std;

const int maxn=1024*1024*10+5;
char ch[maxn],c[maxn];

int main()
{
    int t; cin>>t;
    while(t--){
        int len;
        memset(ch,0,sizeof(ch));
        cin>>len>>ch;
        int num[305],cnt=0;
        memset(num,0,sizeof(num));
        for(int i=0;i<len;i++){
            if(cnt==0){
                num[ch[i]]++;
                c[cnt++]=ch[i];
            }
            else if(ch[i]!=c[cnt-1]){
                num[ch[i]]++;
                c[cnt++]=ch[i];
            }
            else
                num[ch[i]]++;
        }
        if(cnt<3){
            cout<<"NO"<<endl;
            continue;
        }
        bool flag=false;
        for(int i=1;i<cnt-1;i++){
            if(c[i-1]+1==c[i]&&c[i]+1==c[i+1]&&
               num[c[i-1]]>=num[c[i]]&&num[c[i+1]]>=num[c[i]]){
                flag=true;
                break;
            }
        }
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值