#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);
}
如果你們能指出我的其他錯誤在哪裏,或者如果有人能幫我翻譯,我會很感激。
您的編譯器會爲您指出一些錯誤。 –