A string such as "word"
contains the following abbreviations:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
Given a target string and a set of strings in a dictionary, find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary.
Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation "a32bc" has length = 4.
Note:
- In the case of multiple answers as shown in the second example below, you may return any one of them.
- Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20.
Examples:
"apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade") "apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", "3l1").
题解: 回溯。谷歌题目。
class Solution {
public:
string minAbbreviation(string target, vector<string>& dictionary) {
int len=target.size();
if(len==0) return "";
vector<string> dic;
for(string s:dictionary) {
if(s.size()==len) {
dic.push_back(s);
}
}
int len_d=dic.size();
if(len_d==0) return intTostring(len);
string res=target;
dfs("",0,target,0,dic,res,len);
return res;
}
void dfs(string cur,int cur_len,string& target,int pos,vector<string>&dic,string&res,int& minlen) {
if(pos>=(int)target.size()) {
if(cur_len<minlen) { //剪枝
bool f=true;
for(string s:dic) {
if(check(s,cur)) {
f=false;break;
}
}
if(f){
res=cur;
minlen=cur_len;
}
}
return;
}
if(minlen==cur_len) return; //剪枝
if(cur.empty()||!isdigit(cur.back())) { //
for(int i=target.size()-1;i>=pos;i--) {
string add=intTostring(i-pos+1);
dfs(cur+add,cur_len+1,target,i+1,dic,res,minlen);
}
}
dfs(cur+target[pos],cur_len+1,target,pos+1,dic,res,minlen);
}
bool check(string s1,string s2) {
int len1=s1.size();
int len2=s2.size();
int l=0,r=0;
while(l<len1&&r<len2) {
if(isdigit(s2[r])) {
int dis=0;
while(r<len2&&isdigit(s2[r])) {
dis=dis*10+s2[r++]-'0';
}
l+=dis;
}
else if(s2[r]=='0') return false;
else {
if(s1[l]==s2[r]) {
l++;r++;
}
else return false;
}
}
if(l>=len1&&r>=len2) return true;
return false;
}
string intTostring(int num) {
stringstream ss;
ss<<num;
return ss.str();
}
};