凉脾的比赛补题和题解

凉脾的比赛补题和题解_第1张图片

文章目录

  • A Kattis drmmessages
    • 题目
    • 题意
    • 思路
    • 代码
  • B - Game of Throwns
    • 题目
    • 题意
    • 思路
    • 代码
  • C - Sheba's Amoebas
    • 题目
    • 题意
    • 思路
    • 代码

A Kattis drmmessages

题目

DRM Encryption is a new kind of encryption. Given an encrypted string (which we’ll call a DRM message), the decryption process involves three steps: Divide, Rotate and Merge. This process is described in the following example with the DRM message “EWPGAJRB”:

Divide
– First, divide the message in half to “EWPG” and “AJRB”.

Rotate
– For each half, calculate its rotation value by summing up the values of each character (A=0,B=1,…,Z=25). The rotation value of “EWPG” is 4+22+15+6=47. Rotate each character in “EWPG” 47 positions forward (wrapping from Z to A when necessary) to obtain the new string “ZRKB”. Following the same process on “AJRB” results in “BKSC”.

Merge
– The last step is to combine these new strings (“ZRKB” and “BKSC”) by rotating each character in the first string by the value of the corresponding character in the second string. For the first position, rotating ‘Z’ by ‘B’ means moving it forward 1 character, which wraps it around to ‘A’. Continuing this process for every character results in the final decrypted message, “ABCD”.

Input
The input contains a single DRM message to be decrypted. All characters in the string are uppercase letters and the string’s length is even and ≤15000.

Output
Display the decrypted DRM message.

Sample Input 1 Sample Output 1
EWPGAJRB
ABCD
Sample Input 2 Sample Output 2
UEQBJPJCBUDGBNKCAHXCVERXUCVK
ACMECNACONTEST

题意

给你一串大写字母,通过三种方式变换以后问你最后的结果是什么
操作1、把字符串分成两半
操作2、把每一份的字母权重加起来 其中A是0 Z是25,加上以后把两份字母往后转这么多个,一直到达到计算出来的权重。
操作3、把第一份字母加上第二份字母的对应权重,最后得出答案序列。

思路

模拟就行了,如果加上大于Z的话就计算一下差值是多少,然后直接从A开始计算就可

代码

#include 
using namespace std;
int main()
{
    string s;
    cin>>s;
    int len = s.size();
    int sum1 = 0 , sum2 = 0;
    string s1="",s2="";
    for(int i=0;i<len/2;i++)
    s1+=s[i],sum1+=(s[i]-'A');
    sum1%=26;
    for(int i=len/2;i<len;i++)
    s2+=s[i],sum2+=(s[i]-'A');
    sum2%=26;
    // cout<<(char)('A'+sum1-('Z'-'W'+1));
    // cout<
    string s3="";
    for(int i=0;i<s1.size();i++)
    {
        if('Z'-s1[i]>=sum1)
        {
            // cout<<(char)(s1[i]+sum1);
            s3+=(char)(s1[i]+sum1);
        }
        else
        {
            // cout<<(char)('A'+sum1-('Z'-s1[i]+1));
            s3+=(char)('A'+sum1-('Z'-s1[i]+1));
        }
    }
    // cout<
    string s4="";
    for(int i=0;i<s2.size();i++)
    {
        if('Z'-s2[i]>=sum2)
        {
            // cout<<(char)(s2[i]+sum2);
            s4+=(char)(s2[i]+sum2);
        }
        else
        {
            // cout<<(char)('A'+sum2-('Z'-s2[i]+1));
            s4+=(char)('A'+sum2-('Z'-s2[i]+1));
        }
    }
    // cout<
    string ans = "";
    for(int i=0;i<len/2;i++)
    {
        // cout<<'Z'-s3[i]<<" "<
        if('Z'-s3[i]>=s4[i]-'A')
        cout<<(char)(s3[i]+(s4[i]-'A')),ans+=(char)(s3[i]+(s4[i]-'A'));
        else
        cout<<(char)('A'+s4[i]-'A'-('Z'-s3[i]+1)),ans+=(char)('A'+s4[i]-'A'-('Z'-s3[i]+1));
    }
    // cout<
}
// EWPGAJRB
// ZRKB
// BKSC

B - Game of Throwns

题目

Daenerys frequently invents games to help teach her second grade Computer Science class about various aspects of the discipline. For this week’s lesson she has the children form a circle and (carefully) throw around a petrified dragon egg.

The n children are numbered from 0 to n−1 (it is a Computer Science class after all) clockwise around the circle. Child 0 always starts with the egg. Daenerys will call out one of two things:

a number t, indicating that the egg is to be thrown to the child who is t positions clockwise from the current egg holder, wrapping around if necessary. If t is negative, then the throw is to the counter-clockwise direction.

the phrase undo m, indicating that the last m throws should be undone. Note that undo commands never undo other undo commands; they just undo commands described in item 1 above.

For example, if there are 5 children, and the teacher calls out the four throw commands 8 -2 3 undo 2, the throws will start from child 0 to child 3, then from child 3 to child 1, then from child 1 to child 4. After this, the undo 2 instructions will result in the egg being thrown back from child 4 to child 1 and then from child 1 back to child 3. If Daenerys calls out 0 (or n,−n,2n,−2n, etc.) then the child with the egg simply throws it straight up in the air and (carefully) catches it again.

Daenerys would like a little program that determines where the egg should end up if her commands are executed correctly. Don’t ask what happens to the children if this isn’t the case.

Input
Input consists of two lines. The first line contains two positive integers n k (1≤n≤30, 1≤k≤100) indicating the number of students and how many throw commands Daenerys calls out, respectively. The following line contains the k throw commands. Each command is either an integer p (−10000≤p≤10000) indicating how many positions to throw the egg clockwise or undo m (m≥1) indicating that the last m throws should be undone. Daenerys never has the kids undo beyond the start of the game.

Output
Display the number of the child with the egg at the end of the game.

Sample Input 1 Sample Output 1
5 4
8 -2 3 undo 2
3
Sample Input 2 Sample Output 2
5 10
7 -3 undo 1 4 3 -9 5 undo 2 undo 1 6
2

题意

一共有n个学生,k条消息,传一个球。如果一条消息后面没有undo的话,那么就代表着往右传这么多个人,如果一个数后面加了undo,就意味着撤回刚才这么多条消息,问最后球在哪个人手上。

思路

模拟就可,注意要成一个圈,所以在传的时候需要mod一下n
如果最后小于0,就一直加到大于0小于n 最后mod一下n

代码

#include 
#include 
#include 
using namespace std;
const int maxn = 100+10;
int a[maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    int cnt=0,ans=0;
    for(int i=1;i<=k;i++)
    {
        string s;
        cin>>s;
        if(s=="undo")
        {
            int now;
            cin>>now;
            for(int j=cnt;j>=max(cnt-now+1,1);j--)
            {
                ans-=a[j]%n;
                ans%=n;
                // cout<<"undo"<
            }
            cnt-=now;
            if(cnt<0)
            cnt=0;
        }
        else
        {
            stringstream ss;
            int now;
            ss<<s;
            ss>>now;
            a[++cnt]=now;
            ans+=now;
            ans%=n;
        }
        // cout<
    }
    // cout<
    while(ans<0)
    ans+=n;
    cout<<ans%n;
}
/* 5 4
8 -2 3 undo 2
5 10
7 -3 undo 1 4 3 -9 5 undo 2 undo 1 6
*/

C - Sheba’s Amoebas

题目

After a successful Kickstarter campaign, Sheba Arriba has raised enough money for her mail-order biology supply company. “Sheba’s Amoebas” can ship Petri dishes already populated with a colony of those tiny one-celled organisms. However, Sheba needs to be able to verify the number of amoebas her company sends out. For each dish she has a black-and-white image that has been pre-processed to show each amoeba as a simple closed loop of black pixels. (A loop is a minimal set of black pixels in which each pixel is adjacent to exactly two other pixels in the set; adjacent means sharing an edge or corner of a pixel.) All black pixels in the image belong to some loop.

Sheba would like you to write a program that counts the closed loops in a rectangular array of black and white pixels. No two closed loops in the image touch or overlap. One particularly nasty species of cannibalistic amoeba is known to surround and engulf its neighbors; consequently there may be amoebas within amoebas. For instance, each of the images in Figure 1 contains four amoebas.

凉脾的比赛补题和题解_第2张图片

凉脾的比赛补题和题解_第3张图片

Input
The first line of input contains two integers m and n, (1≤m,n≤100). This is followed by m lines, each containing n characters. A ‘#’ denotes a black pixel, a ‘.’ denotes a white pixel. For every black pixel, exactly two of its eight neighbors are also black.

Output
Display a single integer representing the number of loops in the input.

Sample Input 1 Sample Output 1
12 12
.##########.
#…#
#…#…##…#
#.##…#…#.#
#…#.#.#
#…#…#…#
#…#.#…#
#…#…#…#
.#…#.#…#
#…#…#
#…#.
.#########…
4
Sample Input 2 Sample Output 2
12 10
.#####…
#…#…
#…#…#…
#.#.#.#…
#…#…#…
.#…#…
…###…
…#…
.##…#.#…
#…#…#…
.##…

题意

经典的八连块问题

思路

直接dfs染色即可,在一个连通块里面的都染色成一个颜色,再去染别的块。

代码

#include 
using namespace std;
const int maxn = 100+10;
int dx[]={1,-1,0,0,1,1,-1,-1};
int dy[]={0,0,1,-1,1,-1,1,-1};
char mp[maxn][maxn];

int m,n;
bool check(int x,int y)
{
    if(x>=1 && x<=m && y>=1 && y<=n && mp[x][y]=='#')
    return true;
    return false;
}

void dfs(int x,int y)
{
    mp[x][y]='%';
    for(int i=0;i<8;i++)
    {
        int xx = x+dx[i];
        int yy = y+dy[i];
        if(check(xx,yy))
        {
            dfs(xx,yy);
        }
    }
}
int main()
{
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
    cin>>mp[i][j];
    int ans=0;
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
    {
        if(mp[i][j]=='#')
        dfs(i,j),ans++;
    }
    cout<<ans;
}

你可能感兴趣的:(题解)