codeforces---传染性信件

  1. Infectious Letters
    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    You have a string of length n. Unfortunately, some of its letters are infectious. More specifically, any “a” characters in the string have the potential to spread to any adjacent characters. At any point in time, an “a” character in the string can “infect” any of its adjacent characters, and make them an “a” character as well.

However, “b” characters are immune to the “a” characters’ infection, and thus can’t be changed into “a” characters.

For example, if we have the string “cabaccabdd”, the second character of the string could infect the first character, turning it into another “a”, but it couldn’t infect the third character of the string, since it’s a “b”.

Your task is to determine the maximum number of a characters that could exist in the string after an unlimited amount of infections.

For example, in the string “cabaccabdd” could become “aabaaaabdd” in an unlimited number of infections, which has 6 “a” characters.

Input
The first line of input contains a single positive integer n (1<=n<=200000): the length of the string.

The next line of input contains the string.

Output
Output the maximum number of a characters that could exist in the string after an unlimited amount of infections.

Scoring
Full problem: 10 points

翻译:
你有一个长度为n的字符串。不幸的是,它的一些字母具有传染性。更具体地说,字符串中的任何“a”字符都有可能扩展到任何相邻字符。在任意时间点,字符串中的“a”字符可以“感染”它的任何相邻字符,并使它们也成为“a”字符。
而“b”字不受“a”字的感染,因此不能变成“a”字。
例如,如果我们有一个字符串“cabaccabdd”,字符串的第二个字符可以感染第一个字符,把它变成另一个“a”,但它不能感染字符串的第三个字符,因为它是一个“b”。
您的任务是确定在无限数量的感染后,字符串中可能存在的最大字符数。
例如,字符串“cabaccabdd”在无限数量的感染中可能变成“aabaaaabdd”,它有6个“a”字符。 输入
输入的第一行包含一个正整数n (1<=n<=200000):字符串的长度。 下一行输入包含这个字符串。 输出
输出任意数量的感染后字符串中可能存在的最大字符数。

Examples
input1

15
bbbacxdbxyabxab

output1

9

input2

5
bbbbb

output2

0

input3

5
aabcc

output3

2

最终只过了10个测试点,想了半天也不知道test11到底会是什么情况。。。。
test points:

Group main: 0.0 point(s)
    Test 1: OK
    Test 2: OK
    Test 3: OK
    Test 4: OK
    Test 5: OK
    Test 6: OK
    Test 7: OK
    Test 8: OK
    Test 9: OK
    Test 10: OK
    Test 11: WRONG_ANSWER

=
Points: 0.0

error code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    
    
    int n;
    scanf("%d",&n);
    getchar();
    char s[300001];
    scanf("%s",s);

    int sum=0;
    for(int i=0;i<strlen(s);i++){
    
    
        if(s[i]=='a')sum++;//没在两个b之间单独计数
        if(s[i]=='b'){
    
    //在两个b之间或b到字符串结束之间,其它字符全部感染
            i++;
            int sign=0;
            int count=0;
            while(i<strlen(s)&&s[i]!='b'){
    
    //注意i不能越界,到另一个b结束,之间如果有a全部感染
                if(s[i]=='a'){
    
    
                    sign=1;
                }
                i++;
                count++;
            }
            if(sign)sum+=count;//累加
            i--;
        }
    }
    printf("%d",sum);//输出
    return 0;
}

第二天上午有思路了,才发现自己哪里错了,终于ac了!!!
test points:

Group main: 10.0 point(s)
    Test 1: OK
    Test 2: OK
    Test 3: OK
    Test 4: OK
    Test 5: OK
    Test 6: OK
    Test 7: OK
    Test 8: OK
    Test 9: OK
    Test 10: OK
    Test 11: OK
    Test 12: OK
    Test 13: OK
    Test 14: OK
    Test 15: OK
    Test 16: OK
    Test 17: OK
    Test 18: OK
    Test 19: OK
    Test 20: OK
    Test 21: OK
    Test 22: OK
    Test 23: OK
    Test 24: OK
    Test 25: OK
    Test 26: OK
    Test 27: OK
    Test 28: OK
    Test 29: OK
    Test 30: OK
    Test 31: OK
    Test 32: OK
    Test 33: OK
    Test 34: OK
    Test 35: OK
    Test 36: OK
    Test 37: OK
    Test 38: OK
    Test 39: OK
    Test 40: OK
    Test 41: OK

=
Points: 10.0

ac code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
int main()
{
    
    
 
    int n;
    scanf("%d",&n);
    getchar();
 
    char s[200001];
    scanf("%s",s);
 
    for(int i=0;i<n;i++){
    
    
        if(s[i]=='a'){
    
    
            int left=i-1;
            int right=i+1;
 
            while(left>=0&&s[left]!='a'&&s[left]!='b'){
    
    
                s[left--]='a';
            }
 
            while(right<=n-1&&s[right]!='a'&&s[right]!='b'){
    
    
                s[right++]='a';
            }
            i=right-1;
        }
    }
    int num=0;
    for(int i=0;i<n;i++){
    
    
        if(s[i]=='a'){
    
    
            num++;
        }
    }
    printf("%d\n",num);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/timelessx_x/article/details/111770518
今日推荐