題意:
一台聲控機子,有兩條指令:— “repeat the last word”, “delete the last symbol”.給你一些單詞,讓你用合理運用這兩條指令,最少的敲擊鍵盤打出這些單詞。
思路:
很簡單的一道貪心,排序+比較即可。
代碼如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N = 105;
int n;
string str[N];
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
cin >> str[i];
sort(str, str + n);
int cnt = str[0].size();
for (int i = 1; i < n; i++) {
int len = str[i].size();
int j = 0;
while (j < len&& (str[i][j]==str[i - 1][j]))
j++;
cnt += len - j;
}
printf("%d\n", cnt);
for (int i = 0; i < n; i++)
cout<< str[i]<<endl;
}
return 0;
}