結果

問題 No.150 "良問"(良問とは言っていない
ユーザー startcppstartcpp
提出日時 2015-01-30 22:58:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,469 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 55,968 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 09:12:00
合計ジャッジ時間 1,334 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:23:63: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   23 |                                 hamming1 = getHamming(4, S+j ,"good");
      |                                                               ^~~~~~
main.cpp:24:63: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   24 |                                 hamming2 = getHamming(7, S+k, "problem");
      |                                                               ^~~~~~~~~

ソースコード

diff #

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int getHamming(int n, char *str1, char *str2);
int T;
char S[102]; int Slen;
int K[102];

int main() {
	cin >> T;
	for (int i = 0; i < T; i++) {
		K[i] = 50000;			//適当に大きな数を入れておく。(K[i]の最小化が目的。目的にそぐわないものを入れておく。
		cin >> S;
		Slen = strlen(S);
		for (int j = 0; j <= Slen - 11; j++) {
		//j文字目~j+3文字目でgoodを作る(Sの先頭を0文字目とする)
			for (int k = j+4; k <= Slen - 7; k++) {
			//k文字目~k+6文字目でproblemを作る(Sの先頭を0文字目とする)
				int hamming1 = 0;	//goodを作るために必要な操作回数
				int hamming2 = 0;	//problemを作るために必要な操作回数
				hamming1 = getHamming(4, S+j ,"good");
				hamming2 = getHamming(7, S+k, "problem");
				//cout << "j = " << j << " k = " << k << " hamming1 = " << hamming1 << " hamming2 = " << hamming2 << endl;

				K[i] = min(K[i], hamming1 + hamming2);
			}
		}
	}

	for(int i = 0; i < T; i++ )
		cout << K[i] << endl;
	return 0;
}

//長さnの文字列str1,str2について、str1をstr2にするまでに必要な操作回数
//ちなみにこれは、str1とstr2のハミング距離と一致する。
int getHamming(int n, char *str1, char *str2) {
	int ret = 0;
	for (int i = 0; i < n; i++) {
		//異なる文字の場合だけ変更
		if (str1[i] != str2[i])
			ret++;
	}
	return ret;
}
0