-
Notifications
You must be signed in to change notification settings - Fork 0
/
2707.cpp
47 lines (34 loc) · 987 Bytes
/
2707.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
unordered_set<string> store;
vector<int> dp;
int solve(int i, string &s){
int n = s.length();
if(dp[i] != -1) return dp[i];
if(i == n) return dp[i] = 0;
string cur = "";
int res = n;
for(int j = i; j < n; ++j){
cur += s[j];
if(store.find(cur) != store.end()){
res = min(res, 0 + solve(j+1, s));
}
else{
res = min(res, j-i+1 + solve(j+1, s));
}
}
return dp[i] = res;
}
int minExtraChar(string s, vector<string>& d) {
cout << flush;
for(auto s : d) store.insert(s);
int n = s.length();
dp.resize(n+2, -1);
int ans = n;
ans = min(ans, solve(0, s));
dp.clear();
return ans;
}
};