結果

問題 No.9006 マルチバイト文字テスト(テスト用)
ユーザー scrive66scrive66
提出日時 2015-06-04 17:25:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,369 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 52,760 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 19:09:24
合計ジャッジ時間 856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>

/*使用する文字コードに応じて定義する*/
#define SHIFT_JIS
/*#define EUC*/
/*#define UTF_8*/

/*
参考
http://ash.jp/code/code.htm
*/

/*次の一文字のバイト数を判定する*/
/*Shift-JIS用*/
#ifdef SHIFT_JIS
int mozilen_hantei(const unsigned char* moziretu) {
	/*文字列終端*/
	if (moziretu[0] == 0)return 0;
	/*次が文字列終端*/
	if (moziretu[1] == 0)return 1;
	if (
		(
		(moziretu[0] >= 0x81 && moziretu[0] <= 0x9F) ||
		(moziretu[0] >= 0xE0 && moziretu[0] <= 0xEF)
		) && (
		(moziretu[1] >= 0x40 && moziretu[1] <= 0x7E) ||
		(moziretu[1] >= 0x80 && moziretu[1] <= 0xFC)
		)
		)return 2;
	/*その他*/
	return 1;
}
#endif
/*EUC用*/
#ifdef EUC
int mozilen_hantei(const unsigned char* moziretu) {
	/*文字列終端*/
	if (moziretu[0] == 0)return 0;
	/*次が文字列終端*/
	if (moziretu[1] == 0)return 1;
	if (moziretu[0] == 0x8E && (moziretu[1] >= 0xA1 && moziretu[1] <= 0xDF))
		return 2;
	if (
		(moziretu[0] >= 0xA1 && mozireut[0] <= 0xFE) &&
		(moziretu[1] >= 0xA1 && moziretu[1] <= 0xFE)
		)return 2;
	if (
		moziretu[0] == 0z8F &&
		(moziretu[1] >= 0xA1 && moziretu[1] <= 0xFE) &&
		(moziretu[2] >= 0xA1 && moziretu[2] <= 0xFE) &&
		)return 3;
	/*その他*/
	return 1;
}
#endif
/*UTF-8用*/
#ifdef UTF_8
int mozilen_hantei(const unsigned char* moziretu) {
	int max, result;
	for (max = 0; max<6; max++) {
		if (moziretu[max] == 0)break;
	}
	result = 1;
	if ((moziretu[0] & 0x80) == 0x00)result = 1;
	else if ((moziretu[0] & 0xE0) == 0xC0 && max >= 2) {
		if ((moziretu[1] & 0xC0) == 0x80)result = 2;
	} else if ((moziretu[0] & 0xF0) == 0xE0 && max >= 3) {
		if ((moziretu[1] & 0xC0) == 0x80 &&
			(moziretu[2] & 0xC0) == 0x80)result = 3;
	} else if ((moziretu[0] & 0xF8) == 0xF0 && max >= 4) {
		if ((moziretu[1] & 0xC0) == 0x80 &&
			(moziretu[2] & 0xC0) == 0x80 &&
			(moziretu[3] & 0xC0) == 0x80)result = 4;
	} else if ((moziretu[0] & 0xFC) == 0xF8 && max >= 5) {
		if ((moziretu[1] & 0xC0) == 0x80 &&
			(moziretu[2] & 0xC0) == 0x80 &&
			(moziretu[3] & 0xC0) == 0x80 &&
			(moziretu[4] & 0xC0) == 0x80)result = 5;
	} else if ((moziretu[0] & 0xFE) == 0xFC && max >= 6) {
		if ((moziretu[1] & 0xC0) == 0x80 &&
			(moziretu[2] & 0xC0) == 0x80 &&
			(moziretu[3] & 0xC0) == 0x80 &&
			(moziretu[4] & 0xC0) == 0x80 &&
			(moziretu[5] & 0xC0) == 0x80)result = 6;
	}
	if (result>max)result = max;
	return result;
}
#endif

int main(void) {
	std::string inputstr;
	std::string outputstr;
	const char* input;
	int* onelength;
	char* output;
	int i, j, len, inputmax;
	/*入力*/
	std::getline(std::cin, inputstr);
	inputmax = inputstr.size();
	input = inputstr.c_str();
	onelength = new int[inputmax];
	output = new char[inputstr.size() + 100];
	/*半角と全角の判定*/
	for (i = 0; i<inputmax;) {
		len = mozilen_hantei((const unsigned char*)&input[i]);
		onelength[i] = len;
		for (j = 1; j<len; j++)onelength[i + j] = 0;
		if (len <= 0)len = 1;/*無限ループ防止*/
		i += len;
	}
	/*出力文字列の準備*/
	len = 0;
	for (i = inputmax - 1; i >= 0; i--) {
		if (onelength[i]>0) {
			for (j = 0; j<onelength[i]; j++) {
				output[len + j] = input[i + j];
			}
			len += onelength[i];
		}
	}
	output[len] = 0;
	outputstr = output;
	/*メモリ開放*/
	delete onelength;
	delete output;
	/*出力*/
	std::cout << outputstr << std::endl;
	/*正常終了*/
	return 0;
}
0