-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
53 lines (52 loc) · 1.04 KB
/
LCS.cpp
File metadata and controls
53 lines (52 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
int dp[10000][10000];
int LCS(string x){
int m=x.length();
string x2;
for (int i = m-1; i>=0; --i)
{
x2.push_back(x[i]);
}
for (int i = 0; i <=m; ++i)
{
for (int j = 0; j <=m; ++j)
{
if (i==0||j==0)
{
dp[i][j]=0;
}
else if (x[i-1]==x2[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[m][m];
}
std::map<string,char> mp;
std::map<string,int> mpint;
int main(){
int n;
cin>>n;
string s;
string tmp;
char x = 'a';
for (int i = 0; i < n; ++i,++x)
{
cin>>tmp;
if (mpint[tmp]==0)
{
mpint[tmp]++;
mp[tmp]=x;
s.push_back(x);
}
else{
s.push_back(mp[tmp]);
}
}
cout<<s.length()-LCS(s)+1;
}