2.14

[P3205 HNOI2010] 合唱队 - 洛谷 (luogu.com.cn)

import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;

public class Main implements Runnable {

    static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n;
    static final int N = (int) (1e3 + 10);
    static final int mod = 19650827;
    static int[] h = new int[N];
    static int[][][] dp = new int[N][N][2];//0 是左进,1 是右进

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }

    public static int nextInt() throws IOException {
        cin.nextToken();
        return (int) cin.nval;
    }

//    public static String next() throws IOException {
//        return cin.next();
//    }




    @Override
    public void run() {
        try {
            n = nextInt();
            for (int i = 1; i <= n; i++){
                h[i] = nextInt();
                dp[i][i][0] = 1;
            }

            for (int len = 2; len <= n; len++) {
                for (int start = 1; start + len - 1 <= n; start++) {
                    int end = start + len - 1;
                    if (h[start] < h[start + 1]) dp[start][end][0] += dp[start + 1][end][0];
                    if (h[start] < h[end]) dp[start][end][0] += dp[start + 1][end][1];
                    if (h[end] > h[end - 1]) dp[start][end][1] += dp[start][end - 1][1];
                    if (h[end] > h[start]) dp[start][end][1] += dp[start][end - 1][0];
                    dp[start][end][0] %= mod;
                    dp[start][end][1] %= mod;
                }
            }

            cout.println((dp[1][n][0] + dp[1][n][1]) % mod);
            cout.flush();


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

P1140 相似基因 - 洛谷 (luogu.com.cn)

import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;

public class Main implements Runnable {

//    static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static Scanner cin = new Scanner(System.in);
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n, m;
    static final int N = 110;
    static int[] s1 = new int[N];
    static int[] s2 = new int[N];
    static int[][] num = {
            {0, 0, 0, 0, 0, 0},
            {0, 5, -1, -2, -1, -3},
            {0, -1, 5, -3, -2, -4},
            {0, -2, -3, 5, -2, -2},
            {0, -1, -2, -2, 5, -1},
            {0, -3, -4, -2, -1, 0}
    };
    static int[][] dp = new int[N][N];

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }

    public static int nextInt() throws IOException {
        return cin.nextInt();
    }

    public static String next() throws IOException {
        return cin.next();
    }




    @Override
    public void run() {
        try {
            n = nextInt();String s = next();
            for (int i = 1; i <= n; i++) {
                if(s.charAt(i - 1) == 'A') {
                    s1[i] = 1;
                }else if (s.charAt(i - 1) == 'C') {
                    s1[i] = 2;
                }else if (s.charAt(i - 1) == 'G') {
                    s1[i] = 3;
                }else if (s.charAt(i - 1) == 'T') {
                    s1[i] = 4;
                }else {
                    s1[i] = 5;
                }
            }
            m = nextInt();s = next();
            for (int i = 1; i <= m; i++) {
                if(s.charAt(i - 1) == 'A') {
                    s2[i] = 1;
                }else if (s.charAt(i - 1) == 'C') {
                    s2[i] = 2;
                }else if (s.charAt(i - 1) == 'G') {
                    s2[i] = 3;
                }else if (s.charAt(i - 1) == 'T') {
                    s2[i] = 4;
                }else {
                    s2[i] = 5;
                }
            }
            for (int i = 0; i <= n; i++) {
                Arrays.fill(dp[i], -0x3f3f3f3f);
            }
            dp[0][0] = 0;
            for (int i = 1; i <= n; i++) {
                dp[i][0] = dp[i - 1][0] + num[s1[i]][5];
            }
            for (int i = 1; i <= m; i++) {
                dp[0][i] = dp[0][i - 1] + num[5][s2[i]];
            }
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    dp[i][j] = Math.max(dp[i - 1][j - 1] + num[s1[i]][s2[j]],
                            Math.max(dp[i - 1][j] + num[s1[i]][5], dp[i][j - 1] + num[5][s2[j]]));
                }
            }

            cout.println(dp[n][m]);
            cout.flush();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
posted @ 2025-02-15 18:54  Mikkeykarl  阅读(20)  评论(0)    收藏  举报