Meaning: There are n words to be input, and the first word must be input. Now there are two Commands: "repeat the last word" to copy the last word and "delete the last symbol" to delete the last letter of the last word. Ask how many times we need to input at least
Solution: 1: Thinking: Sorting + enumerating each word
2: because there are two commands, copy the last word and delete the last letter of the last word, and can be used infinitely, you only need to sort these words, then, judge the relationship between the current word and the previous word.
Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <set>
Using namespace std;
# Define maxn110
Int t, n, ans;
String str [MAXN];
Void solve (){
Int I, j, k;
Sort (str, str + n); // sort
Ans = str [0]. size ();
For (I = 1; I <n; I ++) {// Enumeration
If (str [I] [0]! = Str [I-1] [0]) {
Ans + = str [I]. size ();
Continue;
}
For (j = 0, k = 0; j <str [I-1]. size (); j ++, k ++ ){
If (str [I-1] [j]! = Str [I] [k])
Break;
}
Ans + = str [I]. size ()-k;
}
Printf ("% d \ n", ans );
For (I = 0; I <n; I ++)
Cout <str [I] <endl;
}
Int main (){
// Freopen ("input.txt", "r", stdin );
Scanf ("% d", & t );
While (t --){
Scanf ("% d", & n );
For (int I = 0; I <n; I ++)
Cin> str [I];
Solve ();
}
Return 0;
}