2つの最長共通の文字列配列を解析(宇宙複雑1)

ここに画像を挿入説明

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1;
	string str2;
	while (cin >> str1 >> str2)
	{
		if (str1.size() < str2.size())
			swap(str1, str2);
		int length1 = str1.size();
		int length2 = str2.size();
		if (length1 == 0 || length2 == 0)
		{
			cout << 0 << endl;
			return 0;
		}
		int maxsize = 0;
		int k = 0;
		for (int i = 0; i < length1; ++i)
		{
			for (int j = 0; j < length2;++j)
			{
				int flag = 0;
				k = i;
				int count = 0;
				while (k < length1 && j < length2 &&str1[k] == str2[j] || str1[k] == str2[j] - 32 || str1[k] == str2[j] + 32)
				{
					++k;
					++j;
					flag = 1;
					++count;
				}
				k = i;
				if (count > maxsize)
				{
					maxsize = count;
				}
				if (flag == 1)
					--j;
			}
		}
		cout << maxsize << endl;
		maxsize = 0;
	}
	return 0;
}

このコードを記述するのに長い時間、1の複素空間のために言っているが、テストケースオフあまりにも多くの牛の詳細は見ることができない、私はあまりにもハード。
コード分析のために:
1。
レッツは、常に最長の文字列が保存されているSTR1
ここに画像を挿入説明
特殊なケースが決定しますがケースを使用していない牛の乗客が同じように見えます
ここに画像を挿入説明
ので、長い文字列が短い文字列を行う外部のサイクリング条件を、行う、ダブルループが続きますサイクル条件
ここに画像を挿入説明

公開された230元の記事 ウォン称賛28 ビュー9335

おすすめ

転載: blog.csdn.net/weixin_43767691/article/details/103302337