• The Festive Evening


    B. The Festive Evening
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in.

    There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.

    For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are k such guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed.

    Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.

    Input

    Two integers are given in the first string: the number of guests n and the number of guards k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26).

    In the second string, n uppercase English letters s1s2... sn are given, where si is the entrance used by the i-th guest.

    Output

    Output «YES» if at least one door was unguarded during some time, and «NO» otherwise.

    You can output each letter in arbitrary case (upper or lower).

    Examples
    Input
    5 1
    AABBB
    Output
    NO
    Input
    5 1
    ABABB
    Output
    YES
    Note

    In the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.

    In the second sample case, the door B is opened before the second guest's arrival, but the only guard can't leave the door A unattended, as there is still one more guest that should enter the castle through this door.

    这题水的很,其实只要找出相同字母第一次出现和最后一次出现的位置,然后判断下就差不多了

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n, k;
     5 char s[1000005];
     6 int l[30], r[30];
     7 int main()
     8 {
     9     scanf("%d%d", &n, &k);
    10     scanf("%s", s + 1);
    11     for(int i = 1; i <= n; i++)
    12     {
    13         if(!l[s[i] - 'A'])
    14         {
    15             l[s[i] - 'A'] = i;
    16         }
    17     }
    18     for(int i = n; i; i--)
    19     {
    20         if(!r[s[i] - 'A'])
    21         {
    22             r[s[i] - 'A'] = i;
    23         }
    24     }
    25     bool flag = 1;
    26     int t = 0;
    27     for(int i = 1; i <= n; i++)
    28     {
    29         if(l[s[i] - 'A'] == i)
    30         {
    31             t++;
    32         }
    33         if(t > k)
    34         {
    35             flag = 0;
    36         }
    37         if(r[s[i] - 'A'] == i)
    38         {
    39             t--;
    40         }
    41     }
    42     if(flag)
    43         printf("NO
    ");
    44     else
    45         printf("YES
    ");
    46 
    47     return 0;
    48 }
  • 相关阅读:
    SQL Server 动态生成分区脚本
    SQL Server数据库服务器高性能设置
    SQL Server 2005 自动化删除表分区设计方案
    SQL Server 自动化管理分区设计方案(图解)
    简单实用SQL脚本Part:sql多行转为一列的合并问题
    简单实用SQL脚本Part9:纵向回填信息
    SQL Server datetime数据类型设计、优化误区
    SQL Server 创建链接服务器
    SQL Server 数据库最小宕机迁移方案
    SQL Server 表分区注意事项
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7266745.html
Copyright © 2020-2023  润新知