c_cpp https://arena.topcoder.com/#/u/practiceCode/13574/7776/8692/1/298183
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp https://arena.topcoder.com/#/u/practiceCode/13574/7776/8692/1/298183相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h>
#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)
using namespace std;
const int MAX = 55;
const int OO = 1e9;
int n, l;
string a[MAX], sorted[MAX];
string s;
int cache[MAX][MAX];
int isValid(int st, int en) {
string sub = s.substr(st, en-st+1);
string tmp = sub;
sort(tmp.begin(), tmp.end());
int indx = -1;
int cost = OO, tmpCost = 0;
lp(i, n) {
tmpCost = 0;
if(tmp == sorted[i]) {
//cout << "DEBUG 2: " << sub << " " << a[i] << endl;
indx = i;
lp(j, sub.length())
if(sub[j] != (a[indx])[j])
tmpCost++;
//cout << "DEBUG tmpCost : " << tmpCost << endl;
cost = min(cost, tmpCost);
}
}
if(indx == -1)
return -1;
//cout << "COOOOOOST " << cost << endl;
return cost;
}
int minDeco(int i, int last) {
if(i == l) {
if(i == last)
return 0;
return OO;
}
int &ret = cache[i][last];
if(ret != -1)
return ret;
ret = OO;
int cost;
if((cost = isValid(last, i)) != -1) {
ret = minDeco(i+1, i+1) + cost;
}
ret = min(ret, minDeco(i+1, last));
return ret;
}
/*
int main() {
clr(cache, -1);
cin>>s;
l = s.length();
cin>>n;
string tmp;
lp(i, n) {
cin>>a[i];
tmp = a[i];
sort(tmp.begin(), tmp.end());
sorted[i] = tmp;
}
int x = minDeco(0, 0);
cout << (x == OO ? -1 : x) << endl;
return 0;
}
*/
class SentenceDecomposition {
public:
int decompose(string s1, vector<string> v) {
clr(cache, -1);
s = s1;
l = s.length();
n = v.size();
string tmp;
lp(i, n) {
a[i] = v[i];
tmp = a[i];
sort(tmp.begin(), tmp.end());
sorted[i] = tmp;
}
int x = minDeco(0, 0);
return ( x == OO ? -1 : x );
}
};
以上是关于c_cpp https://arena.topcoder.com/#/u/practiceCode/13574/7776/8692/1/298183的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 127.单词阶梯
c_cpp MOFSET
c_cpp MOFSET
c_cpp 31.下一个排列
c_cpp string→char *
c_cpp 54.螺旋矩阵