2.把全部相同的連線都找出來,把連線的位置存到陣列。
3.將陣列當區間用,依a小 b大的方式存到陣列,將陣列由小至大排序。
例: 設[1,2] [3,2] [3,4] [2,4]
轉成 [1,2] [2,3] [3,4] [2,4]
排序 [1,2] [2,3] [2,4] [3,4]
4.利用剛轉完的區間,做判斷,連線不交叉的最大配對數為,區間不重疊的最大個數。
(有點數學畫數線的感覺)
例:
(1) BCEAD
BEACD
原 [1,2] [3,2] [4,3] [2,4] [5,5]
轉換並排序 [1,1] [2,3] [2,4] [3,4] [5,5]
取並排的最大值(不重疊,兩端不算)
依單純數學來看,可得到下列可能
[1,1] [2,3] [3,4] [5,5] #4 BEAD
[1,1] [2,4] [5,5] BCD
[1,1] [2,3] [5,5] BED
[1,1] [3,4] [5,5] BAD
(2) BCEAD
BAECD
原 [1,1] [2,4] [3,3] [4,2] [5,5]
轉換並排序 [1,1] [2,4] [2,4] [3,3] [5,5]
[1,1] [2,4] [5,5] #3 BCD
[1,1] [3,3] [5,5] #3 BED
[1,1] [2,4] [5,5] #3 BAD
從此可得知挑的時候,下一個a要大於前一個的a,b要大於前一個的b#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1, s2;
int N, M, max = 0, x = 0, count = 0, ta = 0, tb = 0, tmp = 0, temp = 0;
cin >> N >> M;
getline(cin, s1);
getline(cin, s1);
getline(cin, s2);
int *a = new int[N];
int *b = new int[N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (s1[i] == s2[j])
{
if (j > i)
{
a[x] = i + 1;
b[x] = j + 1;
}
else
{
a[x] = j + 1;
b[x] = i + 1;
}
x++;
}
}
}
if (x != 0)
{
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if ((a[i] > a[j]) || (a[i] == a[j] && b[i] > b[j]))
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
for (int i = 0; i < x; i++)
{
ta = a[i];
tb = b[i];
count = 1;
for (int j = 0; j < x; j++)
{
if (j > i)
{
if (a[j] > ta && b[j] > tb)
{
ta = a[j];
tb = b[j];
count++;
}
}
}
if (count > max)
max = count;
}
}
cout << max << endl;
return 0;
}
沒有留言:
張貼留言