#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int cost[10][10];
string team[10];
int perm[10];
int main() {
int R;
cin >> R;
for (int j=0; j<R; j++) {
cin >> team[j];
perm[j] = j;
}
for (int a=0; a < R; a++)
for (int b=0; b < a; b++) {
string combined = team[a]+team[b];
int overlap = combined.size() - set<char>(combined.begin(), combined.end()).size();
cost[a][b] = cost[b][a] = overlap;
}
int best = 27*R;
do {
int count=0;
for (int j=0; j < R-1; j++)
count += cost[perm[j]][perm[j+1]];
best = min(best,count);
} while (next_permutation(perm,perm+R));
cout << best << endl;
}
выше код на C++, и я не знаю, как перевести эту часть кода: int overlap = combined.size() - set<char>(combined.begin(), combined.end()).size();
Как перевести это небольшое количество кода на C++ на C?
Вот то, что я до сих пор:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int cost[10][10];
char team[10];
int perm[10];
int main() {
int R,j;
scanf("%d", R);
for (j=0; j<R; j++) {
scanf("%d", team[j]);
perm[j] = j;
}
// calculate all-pairs costs
int a , b;
for (a=0; a < R; a++)
for (b=0; b < a; b++) {
strcat(team[a], team[b]);
int overlap = combined.size() - set<char>(combined.begin(), combined.end()).size();
cost[a][b] = cost[b][a] = overlap;
}
// determine best permutation
int best = 27*R;
do {
int count=0;
for (int j=0; j < R-1; j++)
count += cost[perm[j]][perm[j+1]];
best = min(best,count);
} while (next_permutation(perm,perm+R));
printf("%d\n", best);
}
Если вы, ребята, можете укажите, где мои другие ошибки или кто-нибудь может помочь мне перевести это, я был бы очень благодарен.
Ваш компилятор укажет вам некоторые ошибки. –