现在的位置: 首页 > 综合 > 正文

Codeforces Round #290 (Div. 2)

2019年02月19日 ⁄ 综合 ⁄ 共 6772字 ⁄ 字号 评论关闭


    链接:http://codeforces.com/contest/510

A. Fox And Snake

Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.

A snake is a pattern on a n by m table.
Denote c-th cell of r-th row as (r, c).
The tail of the snake is located at (1, 1), then it's body extends to (1, m),
then goes down 2 rows to (3, m), then goes left to (3, 1) and
so on.

Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and
the snake cells should be filled with number signs ('#').

Consider sample tests in order to understand the snake pattern.

Input

The only line contains two integers: n and m (3 ≤ n, m ≤ 50).

n is an odd number.

Output

Output n lines. Each line should contain a string consisting of m characters.
Do not output spaces.

Sample test(s)
input
3 3
output
###
..#
###
input
3 4
output
####
...#
####
input
5 3
output
###
..#
###
#..
###
input
9 9
output
#########
........#
#########
#........
#########
........#
#########
#........
#########

    简单打印图形。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

int n, m;

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int cnt = 1;
        for(int i = 1; i <= n; i ++)
        {
            if(i %2 == 1)
            {
                for(int j = 0; j < m; j ++)
                {
                    printf("#");
                }
            }
            else if(i % 2 == 0 && cnt % 2 == 1)
            {
                cnt ++;
                for(int j = 1; j < m; j ++)
                {
                    printf(".");
                }
                printf("#");
            }
            else if(i % 2 == 0 && cnt % 2 == 0)
            {
                cnt ++;
                printf("#");
                for(int j = 1; j < m; j ++)
                {
                    printf(".");
                }
            }
            printf("\n");
        }
    }
}

    

B. Fox And Two Dots

Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:

Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if
and only if it meets the following condition:

  1. These k dots are different: if i ≠ j then di is
    different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1di and di + 1 are
    adjacent. Also, dk and d1 should
    also be adjacent. Cells x and y are called adjacent
    if they share an edge.

Determine if there exists a cycle on the field.

Input

The first line contains two integers n and m (2 ≤ n, m ≤ 50):
the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters,
expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output

Output "Yes" if there exists a cycle, and "No"
otherwise.

Sample test(s)
input
3 4
AAAA
ABCA
AAAA
output
Yes
input
3 4
AAAA
ABCA
AADA
output
No
input
4 4
YYYR
BYBY
BBBY
BBBY
output
Yes
input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
output
Yes
input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
output
No
Note

In first sample test all 'A' form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above ('Y' = Yellow, 'B'
= Blue, 'R' = Red).

 搜索,判断是否存在环,即相同字母构成的环。我发现,我的搜索还是很弱,递归方面的理解,感觉还是不透彻
 
<span style="color:#222222;">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define N 55

int dis[4][2] = {
    {-1, 0}, {0, 1}, {1, 0}, {0, -1}
};

bool flag = false;
char ss[N][N];
bool vis[N][N];

int n, m;

void dfs(int x, int y, int sx, int sy, char t)
{

    if(x < 1 || x > n || y < 1 || y > m)
        return ;
    if(vis[x][y])
    {
        flag = true;
        return;
    }
    vis[x][y] = true;

    for(int i = 0; i < 4; i ++)
    {
        int xx = x + dis[i][0];
        int yy = y + dis[i][1];
        if(xx < 1 || xx > n || yy < 1 || yy > m)
            continue;
        if(ss[xx][yy] == t)
        {
            if(!(xx == sx && yy == sy))
            dfs(xx, yy, x, y, t);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(ss, '0', sizeof(ss));
        getchar();
        for(int i = 1; i <= n; i ++)
        {
            for(int j = 1; j <= m; j ++)
            {
                scanf("%c",&ss[i][j]);
            }
            getchar();
        }

        flag = false;

        for(int i = 1; i <= n; i ++)
        {
            for(int j = 1; j <= m; j ++)
            {
                memset(vis, false, sizeof(vis));
                dfs(i, j, -1, -1, ss[i][j]);
                if(flag)
                    break;
            }
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");

    }
    return 0;
}</span><span style="color:#33ff33;">
</span>

C. Fox And Names

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.

After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted inlexicographical order in normal sense. But it was always true that after
some modification of the order of letters in alphabet, the order of authors becomes lexicographical!

She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out
any such order.

Lexicographical order is defined in following way. When we compare s and t,
first we find the leftmost position with differing characters:si ≠ ti.
If there is no such position (i. e. s is a prefix of t or
vice versa) the shortest string is less. Otherwise, we compare characters siand ti according
to their order in alphabet.

Input

The first line contains an integer n (1 ≤ n ≤ 100):
number of names.

Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100),
the i-th name. Each name contains only lowercase Latin letters. All names are different.

Output

If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).

Otherwise output a single word "Impossible" (without quotes).

Sample test(s)
input
3
rivest
shamir
adleman
output
bcdefghijklmnopqrsatuvwxyz
input
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
output
Impossible
input
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
output
acbdefhijklmnogpqrstuvwxyz

意思是,题目输出的是按照“特别”的字母顺序表排序的,而你就是要输出这个字母顺序表,如果不存在,就输出impossible。可以用拓扑排序来做,构建一个有向连通图。我跪在了第十三组数据,跪了好久,就过是CF的正确答案和我自己的答案看反掉了,到最后发现是continue的位置放错了- -#

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 110

int in[N];
int ans[N];
char tt[N][N];
bool mpt[N][N];
int cnt;

bool topo()
{
    cnt = 0;
    for(int i = 0; i < 26; i ++)
    {
        int k = 0;
        while(in[k] != 0 && k < 26)
            k ++;
        if(k == 26)
            return false;
        ans[cnt ++] = k;
        in[k] = -1;
        for(int j = 0; j < 26; j ++)
        {
            if(mpt[k][j])
                in[j] --;
        }
    }
    if(cnt < 26)
        return false;
    else
        return true;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(tt, '0', sizeof(tt));
        memset(mpt, false, sizeof(mpt));
        memset(in, 0, sizeof(in));
        memset(ans, 0, sizeof(ans));

        for(int i = 0; i < n; i ++)
        {
            scanf("%s",tt[i]);
        }
        bool flag = true;
        for(int i = 0; i < n - 1; i ++)
        {
            int sta = 0;
            int len1 = strlen(tt[i]);
            int len2 = strlen(tt[i + 1]);
            while(tt[i][sta] == tt[i + 1][sta] &&(sta < min(len1, len2)))
                sta ++;
            if(sta == min(len1, len2) && len1 > len2)  // 处理类似第十三组这样的数据的数据
            {

                flag = false;
                continue;
            }

            if(!mpt[tt[i][sta] - 'a'][tt[i + 1][sta] - 'a'])  // 这步不可以少,不然 会重复计数
            {
                mpt[tt[i][sta] - 'a'][tt[i + 1][sta] - 'a'] = true;
                in[tt[i + 1][sta] - 'a'] ++;
            }
        }
        cnt = 0;
        if(!flag)
        {
            printf("Impossible\n");
            continue;;
        }

        flag = topo();
        if(flag)
        {
            for(int i  = 0; i < cnt; i ++)
            {
                printf("%c",ans[i] + 'a');
            }
        }
        else
            printf("Impossible");
        printf("\n");
    }
}

/**************

献上第13组数据  Orz.... >.<

INPUT
2
aa
a

OUTPUT
Impossible

*****/

抱歉!评论已关闭.