Codeforces Round #817 (Div. 4) (关于你能AK的一场比赛)

本弱弱很菜,大佬请指教

A. Spell Check

 签到题:

题意: 给你一个字符串,判断是否又上述的名字字符组成,要求有一个T,其他的都为小写

AC代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        String ans = "Timru";
        for (int tt = 0; tt < t; tt++) {
            int n = sc.nextInt();
            String s = sc.next();
            char[] ss = s.toCharArray();
            Arrays.sort(ss);

            String x = new String(ss);
            if (x.equals(ans)) System.out.println("yes");
            else System.out.println("no");
        }
    }
}

B. Colourblindness

 

继续签到题:

题意: 一个2 * n的一个网格,由B,G,R组成,判断两行是否相同,B,G可以忽略

AC代码

 
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int tt = 0; tt < t; tt++) {
            int n = sc.nextInt();
            String a = sc.next();
            String b = sc.next();

            boolean isTrue = true;
            for (int i = 0; i < a.length(); i++) {
                if (a.charAt(i) == 'R'){
                    if (b.charAt(i) != 'R'){
                        isTrue = false;
                        break;
                    }
                }
                if (b.charAt(i) == 'R'){
                    if (a.charAt(i) != 'R'){
                        isTrue = false;
                        break;
                    }
                }
            }
            if (isTrue) System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

C. Word Game

 继续签到:

题意:有三个人,每个人写下N 个不同的单词,假如写下这个单词只有一个人写,得3分,两个人写得1分,否则不得分

思路:用HashMap存起来个数,遍历一次就ok

AC代码:

 
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int tt = 0; tt < t; tt++) {
            int n = sc.nextInt();
            String [][] s = new String[3][n];
            Map<String,Integer> hm = new HashMap<>();
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < n; j++) {
                    s[i][j] = sc.next();
                    hm.put(s[i][j],hm.getOrDefault(s[i][j],0) + 1);
                }
            }

            for (int i = 0; i < 3; i++) {
                int score = 0;
                for (int j = 0; j < n; j++) {
                    if (hm.get(s[i][j]) == 1) score += 3;
                    else if (hm.get(s[i][j]) == 2) score++;
                }
                System.out.print(score + " ");
            }
            System.out.println();
        }
    }
}

D. Line

题意:给你一个字符串,只有L,R组成,每个字母有一定的权值,假如是L的话,权值为左边字母的个数,R的话,权值为右边字母的个数 ,你有 k(1 ~ n)次修改字母的机会,输出1 ~ n次修改的最大权值结果

思路: 把每一个位置修改的结果存下来,然后排序,从大到小,贪心遍历即可

AC代码

 
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0){
            int n = sc.nextInt();

            String s = sc.next();
            Integer [] a = new Integer[n];
            long sum = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'L'){
                    sum += i;
                    a[i] = (n - i - 1) - i;
                }
                else {
                    sum += n - i - 1;
                    a[i] = i - (n - i - 1);
                }
            }
            Arrays.sort(a, (o1, o2) -> o2 - o1);
            for (int i = 0; i < n; i++) {
                if (a[i] <= 0){
                    for (int j = i; j < n; j++) System.out.print(sum + " ");
                    break;
                }
                sum += a[i];
                System.out.print(sum + " ");
            }
            System.out.println();
        }
    }
}

F. L-shapes

 

题意:给你一个矩形,让你判断是否满足要求,题目给出了L的合法形状,每个 * 都是一个L的一部分,任意两个L,在八个方向不能相邻,则符合要求

思路: dfs去判断周围的 * 是否是只有三个,然后三个坐标必须 "相邻”

AC代码

 
import java.util.*;

public class Main {
    public static boolean [][] vis;
    public static char [][] g;
    public static int [] dx = new int[] {-1,-1,-1,0,0,1,1,1};
    public static int [] dy = new int[] {-1,0,1,1,-1,-1,0,1};
    public static int n,m;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0){
            n = sc.nextInt();
            m = sc.nextInt();

            g = new char[n][m];
            vis = new boolean[n][m];
            for (int i = 0; i < n; i++) {
                g[i] = sc.next().toCharArray();
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (g[i][j] == '.') vis[i][j] = true;
                }
            }
            boolean flag = true;
            to:
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (vis[i][j]) continue ;
                    vis[i][j] = true;
                    List<int[]> list = new ArrayList<>();
                    list.add(new int[] {i,j});

                    dfs(i,j,list);
                    if (list.size() != 3){
                        flag = false;
                        break to;
                    }
                    int maxX = -1, minX = 100, maxY = -1, minY =100;
                    for (int k = 0; k < 3; k++) {
                        maxX = Math.max(maxX, list.get(k)[0]);
                        minX = Math.min(minX, list.get(k)[0]);
                        maxY = Math.max(maxY, list.get(k)[1]);
                        minY = Math.min(minY, list.get(k)[1]);

                    }
                    if(maxY-minY!=1 || maxX-minX!=1){
                        flag = false;
                        break to;
                    }
                }
            }
            if (flag) System.out.println("Yes");
            else System.out.println("No");
        }
    }

    private static void dfs(int x, int y, List<int[]> list) {
        for (int i = 0; i < 8; i++) {
            int fx = x + dx[i];
            int fy = y + dy[i];
            if (fx >= n || fx < 0 || fy >= m || fy < 0) continue;
            if(vis[fx][fy]) continue;
            vis[fx][fy] = true;
            if (g[fx][fy] == '*'){
                list.add(new int[] {fx,fy});
                dfs(fx,fy,list);
            }
        }
    }
}

G. Even-Odd XOR

 

题意:给你一个数 n  ,让你构造出奇数下标的异或值等于偶数下标的异或值

思路:奇数下标的异或值为 a,偶数下标的异或值为 b,那么a == b,a ^ b = 0

所以所有数的异或值等于零,那么我们是不是更好构造了,由于每个数都不能相同,我可以留三个位置出来,前面随便构造,最后一个放前 n - 1异或值相等的数,另外放两个较大的数即可

AC代码:

 
import java.util.*;

public class Main {
    public static int n,m;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0){
            n = sc.nextInt();
            int [] ans = new int[n];

            ans[n - 3] = 1 << 29;
            ans[n - 2] = 1 << 30;

            int xor = ans[n - 3] ^ ans[n - 2];
            for (int i = 0; i < n - 3; i++) {
                ans[i] = i;
                xor ^= i;
            }
            ans[n - 1] = xor;
            for (int i = 0; i < n; i++) {
                System.out.print(ans[i] + " ");
            }
            System.out.println();
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值