結果

問題 No.874 正規表現間距離
ユーザー QCFiumQCFium
提出日時 2019-08-30 23:02:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,413 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 174,164 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-11-22 02:30:37
合計ジャッジ時間 3,185 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/vector:64,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/queue:61,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:86,
                 from main.cpp:1:
In member function 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>]',
    inlined from 'int main()' at main.cpp:31:9:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_vector.h:1124:32: warning: '*(std::vector<int, std::allocator<int> >*)dp.std::vector<int>::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int> >::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_Vector_impl_data::_M_start' may be used uninitialized [-Wmaybe-uninitialized]
 1124 |         return *(this->_M_impl._M_start + __n);
      |                  ~~~~~~~~~~~~~~^~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	assert(scanf("%d", &n) == 1);
	return n;
}
struct D {
	int c;
	bool one_more;
	bool more;
};
int main () {
	std::string a_, b_;
	std::cin >> a_ >> b_;
	std::vector<D> a, b;
	for (auto c : a_) {
		if (c == '*') a.back().more = true;
		else if (c == '?') a.back().one_more = true;
		else a.push_back({c - 'a', false, false});
	}
	for (auto c : b_) {
		if (c == '*') b.back().more = true;
		else if (c == '?') b.back().one_more = true;
		else b.push_back({c - 'a', false, false});
	}
	int n = a.size();
	int m = b.size();
	std::vector<int> dp[n + 1];
	for (int i = 0; i <= n; i++) dp[i].resize(m + 1, 1000000000);
	dp[0][0] = 0;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			if (i < n && j < m) {
				int a_add = !a[i].more;
				int b_add = !b[j].more;
				int cost = a[i].c != b[j].c;
				dp[i + a_add][j + b_add] = std::min(dp[i + a_add][j + b_add], dp[i][j] + cost);
			}
			if (i < n) {
				int cost = 1;
				if (a[i].one_more || a[i].more) cost = 0;
				else if (j < m && b[j].c == a[i].c && b[j].more) cost = 0;
				dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + cost);
			}
			if (j < m) {
				int cost = 1;
				if (b[j].one_more || b[j].more) cost = 0;
				else if (i < n && a[i].c == b[j].c && a[i].more) cost = 0;
				dp[i][j + 1] = std::min(dp[i][j + 1], dp[i][j] + cost);
			}
		}
	}
	std::cout << dp[n][m] << std::endl;
	return 0;
}
0