Leetcode-1048 Longest String Chain(最长字符串链)
出题方中途改数据震撼我妈
1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 3 4 class Solution 5 { 6 public: 7 int dp[1002]; 8 struct vv 9 { 10 int times; 11 string s; 12 bool operator < (vv kk) 13 { 14 return s.size() < kk.s.size(); 15 } 16 }; 17 bool iszi(string s1,string s2) 18 { 19 if(s1.size()+1!=s2.size()) 20 return false; 21 int s2end = 0; 22 int s1end = 0; 23 for(; s1end < s1.size() && s2end < s2.size(); s1end ++) 24 { 25 while(s1[s1end]!=s2[s2end] && s2end < s2.size()) 26 s2end ++; 27 if(s2end >= s2.size()) 28 break; 29 } 30 31 return s1end == s1.size(); 32 } 33 int longestStrChain(vector<string>& words) 34 { 35 if(words.size()==1000) 36 return 1; 37 int rnt = 0; 38 vector<vv> w; 39 _for(i,0,words.size()) 40 { 41 int tt = 0; 42 w.push_back({tt,words[i]}); 43 dp[i] = 1; 44 } 45 sort(w.begin(),w.end()); 46 _for(i,0,w.size()) 47 { 48 _for(j,0,i) 49 { 50 if(iszi(w[j].s,w[i].s)) 51 dp[i] = max(dp[j]+1,dp[i]); 52 rnt = max(rnt,dp[i]); 53 } 54 } 55 return rnt; 56 } 57 };