• Codeforces Round #302 (Div. 2)


    A. Set of Strings
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.

    Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.

    Input

    The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.

    The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.

    Output

    If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.

    If there are multiple possible answers, print any of them.

    Sample test(s)
    input
    1
    abca
    output
    YES
    abca
    input
    2
    aaacas
    output
    YES
    aaa
    cas
    input
    4
    abc
    output
    NO
    Note

    In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}.

    B. Sea and Islands
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).

    Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists.

    Input

    The single line contains two positive integers nk (1 ≤ n ≤ 100, 0 ≤ k ≤ n2) — the size of the map and the number of islands you should form.

    Output

    If the answer doesn't exist, print "NO" (without the quotes) in a single line.

    Otherwise, print "YES" in the first line. In the next n lines print the description of the map. Each of the lines of the description must consist only of characters 'S' and 'L', where 'S' is a cell that is occupied by the sea and 'L' is the cell covered with sand. The length of each line of the description must equal n.

    If there are multiple answers, you may print any of them.

    You should not maximize the sizes of islands.

    Sample test(s)
    input
    5 2
    output
    YES
    SSSSS
    LLLLL
    SSSSS
    LLLLL
    SSSSS
    input
    5 25
    output
    NO
    C. Writing Code
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes.

    Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v2 + ... + vn = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v1 lines of the given task, then the second programmer writes v2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most bbugs in total.

    Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.

    Input

    The first line contains four integers nmbmod (1 ≤ n, m ≤ 500, 0 ≤ b ≤ 500; 1 ≤ mod ≤ 109 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

    The next line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of bugs per line for each programmer.

    Output

    Print a single integer — the answer to the problem modulo mod.

    Sample test(s)
    input
    3 3 3 100
    1 1 1
    output
    10
    input
    3 6 5 1000000007
    1 2 3
    output
    0
    input
    3 5 6 11
    1 2 1
    output
    0
    D. Destroying Roads
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a andb are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.

    You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.

    Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

    Input

    The first line contains two integers nm (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively.

    Next m lines contain the descriptions of the roads as pairs of integers aibi (1 ≤ ai, bi ≤ nai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.

    The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n0 ≤ li ≤ n).

    Output

    Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

    Sample test(s)
    input
    5 4
    1 2
    2 3
    3 4
    4 5
    1 3 2
    3 5 2
    output
    0
    input
    5 4
    1 2
    2 3
    3 4
    4 5
    1 3 2
    2 4 2
    output
    1
    input
    5 4
    1 2
    2 3
    3 4
    4 5
    1 3 2
    3 5 1
    output
    -1
    E. Remembering Strings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i.

    For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because:

    • the first string is the only string that has character c in position 3;
    • the second string is the only string that has character d in position 2;
    • the third string is the only string that has character s in position 2.

    You want to change your multiset a little so that it is easy to remember. For aij coins, you can change character in the j-th position of the i-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.

    Input

    The first line contains two integers nm (1 ≤ n, m ≤ 20) — the number of strings in the multiset and the length of the strings respectively. Next n lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is m.

    Next n lines contain m integers each, the i-th of them contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 106).

    Output

    Print a single number — the answer to the problem.

    Sample test(s)
    input
    4 5
    abcde
    abcde
    abcde
    abcde
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    output
    3
    input
    4 3
    abc
    aba
    adc
    ada
    10 10 10
    10 1 10
    10 10 10
    10 1 10
    output
    2
    input
    3 3
    abc
    ada
    ssa
    1 1 1
    1 1 1
    1 1 1
    output
    0

    A题:

    题目大意:

      题目就是说,给你一个字符串,然后让你把他分割成k个小段,要求这k个小段的字符串的首字母不能够相同,如果有方案就输出任意一种。

    解题思路:

      上来后,看到是字符串的题目,而且只包含小写字母,想都没想,直接把他们全部映射成 a->1,b->2,c->3....

      然后,我们统计输入的q串中有多少种不同类型的字母,如果不同的类型字母<k的话,,那么绝壁是不可能的。因为任意几个字符串的组合肯定会有两个字符串的首字母是相同的。

      接下来我们优先一个一个字符的输出,也就是说, 让一个字符当做k个子段中的某个子段,当剩下第k个子段的时候,我们就把剩余的单词补刀某个单词的后面就ok啦。。。

      边输出边记录,看看那些单词已经被我们输出了,那些还没有。。。

    代码:

    # include<cstdio>
    # include<iostream>
    # include<cstring>
    
    using namespace std;
    
    # define MAX 123
    
    int hsh[MAX];
    char ss[MAX];
    
    
    int main(void)
    {
        int k;
        while ( scanf("%d",&k)!=EOF )
        {
            memset(hsh,0,sizeof(hsh));
            scanf("%s",ss);
            int len = strlen(ss);
            if ( len < k )
            {
                printf("NO
    ");
                continue;
            }
    
            for ( int i = 0;i < len;i++ )
            {
                hsh[ss[i]-'a'+1] = 1;
            }
            int cnt = 0;
            for ( int i = 0;i < MAX;i++ )
            {
                if ( hsh[i] == 1 )
                {
                    cnt++;
                }
            }
            if ( cnt < k )
            {
                printf("NO");
                continue;
            }
            printf("YES");
            int tot = 0;
            for ( int i = 0;i < len;i++ )
            {
                if ( hsh[ss[i]-'a'+1]==1&&tot < k )
                {
                    printf("
    ");
                    tot++;
                    hsh[ss[i]-'a'+1]=0;
                }
                printf("%c",ss[i]);
            }
            printf("
    ");
    
        }
    
        return 0;
    }

      

    B题:

      题目大意:

        意思就是说,给你一个n*n全部是S组成的矩阵,然后,让你用L去替换矩阵中的S,使得最后出现L的联通块的个数等于k。

      解题思路:

        简单的构造问题,上来后,只要求出在一个n*n的矩阵中最多能填写多少个L就行了,我们根据n的奇偶性,算出ans_max,就好,一开始算错了这个,WA了一发。。。

      然后,在构造这个矩阵的过程中,我们只需要在i和j同奇偶性的地方填写L,其余地方填写S。。

      代码:

     1 # include<cstdio>
     2 # include<iostream>
     3 # include<cmath>
     4 
     5 using namespace std;
     6 
     7 # define MAX 123
     8 
     9 char grid[MAX][MAX];
    10 
    11 int main(void)
    12 {
    13     int n,k,ans_max;
    14     while ( scanf("%d%d",&n,&k)!=EOF )
    15     {
    16         if ( n%2==1 )
    17             ans_max = (ceil(n*n/2.0));
    18         else
    19             ans_max = n*n/2;
    20         if ( k > ans_max )
    21         {
    22             printf("NO
    ");
    23             continue;
    24         }
    25         printf("YES
    ");
    26         int cnt = 0;
    27         for ( int i = 0;i < n;i++ )
    28         {
    29             for ( int j = 0;j < n;j++ )
    30             {
    31                 if ( i%2==j%2 && cnt < k)
    32                 {
    33                     cnt++;
    34                     printf("L");
    35                 }
    36                 else
    37                     printf("S");
    38             }
    39             puts(" ");
    40         }
    41 
    42 
    43     }
    44 
    45 
    46     return 0;
    47 }

    C题:

      题目大意:

        意思就是说,有n个程序员,每个程序员打代码的时候会出现a[i]个bug,然后每个人能打v[i]行的代码,使得v[1]+v[2]+...+v[n] = m,且a[1]+a[2]+a[3]+...+a[n]<=b

      的方案数有多少种,最后记着边算边mod。

      解题思路:

        典型的背包问题,直接套dp[i][j][k]+=dp[i][j-1][k-a[i]];

        定义状态:dp[i][j][k] 表示的是前i个人,总共打j行代码,出现k个bug的方案数目.

        状态转移方程:dp[i%2][j][k] = dp[(i+1)%2][j][k]; 

               dp[i%2][j][k] += dp[i%2][j-1][k-a[i]];    k >= a[i]&&j>0 

        边界条件:dp[0][0][0] = 1;

        由于dp[i][j][k]放不下这么大的数组,所以,我们一开始只需要先滚动下就好,把他变成0和1,就是上一个状态和当前的状态。

      代码:

     1 # include<cstdio>
     2 # include<iostream>
     3 # include<cstring>
     4 
     5 using namespace std;
     6 
     7 # define MAX 555
     8 
     9 int a[MAX];
    10 int dp[2][MAX][MAX];
    11 
    12 int main(void)
    13 {
    14     int n,m,b,MOD;
    15     while ( scanf("%d%d%d",&n,&m,&b)!=EOF )
    16     {
    17         scanf("%d",&MOD);
    18         for ( int i = 1;i <= n;i++ )
    19         {
    20             scanf("%d",&a[i]);
    21         }
    22         dp[0][0][0] = 1;
    23         for ( int i = 1;i <= n;i++ )
    24         {
    25             for ( int j = 0;j <= m;j++ )
    26             {
    27                 for ( int k = 0;k <= b;k++ )
    28                 {
    29                     dp[i%2][j][k] = dp[(i+1)%2][j][k];
    30                     if ( j > 0&& k >= a[i] )
    31                     {
    32                         dp[i%2][j][k] = (dp[i%2][j][k]+dp[i%2][j-1][k-a[i]])%MOD;
    33                     }
    34                 }
    35             }
    36         }
    37 
    38         int ans = 0;
    39         for ( int i = 0;i <= b;i++ )
    40         {
    41             ans = ( ans+dp[n%2][m][i] )%MOD;
    42         }
    43         printf("%d
    ",ans);
    44         memset(dp,0,sizeof(dp));
    45     }
    46 
    47 
    48     return 0;
    49 }

    D题:

      去吃饭了。。。下午一下午的实验,晚上回来有时间了再写。。

  • 相关阅读:
    Vue生命周期
    事件
    改变this指向
    全屏要素
    微信小程序
    编程心得
    设计模式-单例与实例化
    第五节课 课堂总结
    作业四 分支结构
    课堂总结
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4487409.html
Copyright © 2020-2023  润新知