結果

問題 No.225 文字列変更(medium)
ユーザー h sakh sak
提出日時 2020-11-04 01:23:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 5,471 bytes
コンパイル時間 2,685 ms
コンパイル使用メモリ 74,744 KB
実行使用メモリ 61,128 KB
最終ジャッジ日時 2023-09-29 15:19:30
合計ジャッジ時間 5,193 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
53,804 KB
testcase_01 AC 84 ms
56,148 KB
testcase_02 AC 41 ms
49,080 KB
testcase_03 AC 41 ms
49,112 KB
testcase_04 AC 42 ms
49,044 KB
testcase_05 AC 41 ms
49,436 KB
testcase_06 AC 43 ms
49,204 KB
testcase_07 AC 42 ms
49,048 KB
testcase_08 AC 41 ms
48,940 KB
testcase_09 AC 42 ms
49,092 KB
testcase_10 AC 42 ms
49,116 KB
testcase_11 AC 42 ms
49,128 KB
testcase_12 AC 88 ms
56,488 KB
testcase_13 AC 89 ms
61,128 KB
testcase_14 AC 89 ms
56,156 KB
testcase_15 AC 87 ms
56,216 KB
testcase_16 AC 88 ms
56,400 KB
testcase_17 AC 87 ms
56,700 KB
testcase_18 AC 87 ms
56,152 KB
testcase_19 AC 87 ms
56,200 KB
testcase_20 AC 87 ms
56,248 KB
testcase_21 AC 88 ms
56,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;


public class Main {
	//https://qiita.com/drken/items/a5e6fe22863b7992efdb
	//#10
	public static void main(String[] args) {

		FastScanner scan = new FastScanner();
		long mod = 998244353L;

		int n = scan.nextInt();
		int m = scan.nextInt();
//		long k = scan.nextLong();

//		long[] a = new long[n];
//		long[] v = new long[n];
//		long[] z = new long[n];

//		int bit = 0;
		String s = scan.next();
		String t = scan.next();

		long [][] dp = new long[s.length()+1][t.length()+1];
		//dp[s.length][t.length] : Sのs.length文字までとTの・・・での最長ぶぶん文字列

//		long count = 0L;
//		double min = 200;
//		for (int i = 0; i < n; i++) {
//			a[i] = scan.nextLong();
//			v[i] = scan.nextLong();
//			z[i] = scan.nextLong();
//		}

		dp[0][0]= 0L;
		for (int i = 1; i <= s.length(); i++) {
			dp[i][0] = i;
		}

		for (int i = 1; i <= t.length(); i++) {
			dp[0][i] = i;
		}

		for (int i = 0; i < s.length(); i++) {
			for (int j = 0; j < t.length(); j++) {
				if(s.charAt(i)==t.charAt(j)) {
					dp[i+1][j+1] = dp[i][j];
				} else {
					dp[i+1][j+1] = Math.min(dp[i][j]+1, dp[i+1][j]+1);
					dp[i+1][j+1] = Math.min(dp[i+1][j+1], dp[i][j+1]+1);
				}
			}
		}

		System.out.println(dp[s.length()][t.length()]);
	}

	static long dp(int bit, int now, long[][] dp , int n, long[]x, long[]y, long[]z) {
		if(dp[bit][now] >0) {
			return dp[bit][now];
		}

		if (bit == (1<<n) -1) {
			long tmp = Math.abs(x[0] -x[now]) + Math.abs(y[0] - y[now]) + Math.max(0, z[0]-z[now]);
			dp[bit][now]= tmp;
			return tmp;
		}
		long min = Long.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			if((bit & 1<<i) != 0) continue;
			long d = dp(bit | 1<<i, i, dp, n, x, y, z);
			long tmp = d + Math.abs(x[i] -x[now]) + Math.abs(y[i] - y[now]) + Math.max(0, z[i]-z[now]);
			min = tmp < min ? tmp : min;
		}
		dp[bit][now]= min;
		return min;
	}

	static boolean nextPermutation(int[] a) {
		int n=a.length;
		int p=n-1;
		while (p-1>=0 && a[p-1]>a[p]) --p;
		if (p==0) return false;
		int q=p;
		while (q+1<n && a[p-1]<a[q+1]) ++q;
		swap(p-1,q,a);
		int s=p;
		int t=n-1;
		while (s<t) swap(s++,t--,a);
		return true;
	}
	static void swap(int i,int j,int[] a) {
		a[i]^=a[j];a[j]^=a[i];a[i]^=a[j];
	}

//	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}


	private static class DSU {
		int[] root;
		int[] height;
//		long[] a; //customizable
//		long[] b; //customizable
		int n;

		DSU(int n) {
			this.n = n;
			root = new int[n];
			height = new int[n];
//			a = new long[n];
//			b = new long[n];
			for(int i = 0; i < n; i++)
				root[i] = i;
		}

		int findParent(int idx) {
			if(root[idx] != idx) {
				root[idx] =  findParent(root[idx]);
				return root[idx];
			}
			return idx;
		}

		boolean union(int x, int y) {
			int parX = findParent(x);
			int parY = findParent(y);
			if(parX == parY) return false;
			if(height[parX] < height[parY]) {
				root[parY] = parX;
				height[parX] += height[parY]+1;
//				a[parX] +=a[parY]; //customizable
//				b[parX] +=b[parY]; //customizable
			} else {
				root[parX] = parY;
				height[parY] += height[parX]+1;
//				a[parY] +=a[parX]; //customizable
//				b[parY] +=b[parX]; //customizable
			}
			return true;
		}
	}

	static class FastScanner {
	    private final InputStream in = System.in;
	    private final byte[] buffer = new byte[1024];
	    private int ptr = 0;
	    private int buflen = 0;
	    private boolean hasNextByte() {
	        if (ptr < buflen) {
	            return true;
	        }else{
	            ptr = 0;
	            try {
	                buflen = in.read(buffer);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            if (buflen <= 0) {
	                return false;
	            }
	        }
	        return true;
	    }
	    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
	    public String next() {
	        if (!hasNext()) throw new NoSuchElementException();
	        StringBuilder sb = new StringBuilder();
	        int b = readByte();
	        while(isPrintableChar(b)) {
	            sb.appendCodePoint(b);
	            b = readByte();
	        }
	        return sb.toString();
	    }
	    public long nextLong() {
	        if (!hasNext()) throw new NoSuchElementException();
	        long n = 0;
	        boolean minus = false;
	        int b = readByte();
	        if (b == '-') {
	            minus = true;
	            b = readByte();
	        }
	        if (b < '0' || '9' < b) {
	            throw new NumberFormatException();
	        }
	        while(true){
	            if ('0' <= b && b <= '9') {
	                n *= 10;
	                n += b - '0';
	            }else if(b == -1 || !isPrintableChar(b)){
	                return minus ? -n : n;
	            }else{
	                throw new NumberFormatException();
	            }
	            b = readByte();
	        }
	    }
	    public int nextInt() {
	        long nl = nextLong();
	        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
	        return (int) nl;
	    }
	    public double nextDouble() { return Double.parseDouble(next());}
	}
}

0