結果

問題 No.225 文字列変更(medium)
ユーザー h sakh sak
提出日時 2020-11-04 01:23:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 115 ms / 5,000 ms
コード長 5,471 bytes
コンパイル時間 3,389 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 49,980 KB
最終ジャッジ日時 2024-07-22 09:30:00
合計ジャッジ時間 6,174 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
41,292 KB
testcase_01 AC 110 ms
45,692 KB
testcase_02 AC 53 ms
36,468 KB
testcase_03 AC 53 ms
36,552 KB
testcase_04 AC 53 ms
36,168 KB
testcase_05 AC 52 ms
36,644 KB
testcase_06 AC 54 ms
36,748 KB
testcase_07 AC 51 ms
36,596 KB
testcase_08 AC 52 ms
36,588 KB
testcase_09 AC 52 ms
36,736 KB
testcase_10 AC 55 ms
36,732 KB
testcase_11 AC 54 ms
36,556 KB
testcase_12 AC 109 ms
45,724 KB
testcase_13 AC 115 ms
49,980 KB
testcase_14 AC 109 ms
45,936 KB
testcase_15 AC 109 ms
45,752 KB
testcase_16 AC 112 ms
45,964 KB
testcase_17 AC 110 ms
45,620 KB
testcase_18 AC 103 ms
45,956 KB
testcase_19 AC 109 ms
45,632 KB
testcase_20 AC 110 ms
45,496 KB
testcase_21 AC 111 ms
45,768 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