spider68

q1=>longest string chain

May 3rd, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. int longestStrChain(vector<string>& words) {
  2.       sort(words.begin(),words.end(),[](string a,string b){return a.length()<b.length();});
  3.         int sum=0;
  4.         map<string,int>dp;
  5.         int i,j;
  6.        
  7.         for(string s:words)
  8.         {
  9.             for(i=0;i<s.length();i++)
  10.             {
  11.                 dp[s]=max(1+dp[s.substr(0,i)+s.substr(i+1)],dp[s]);  
  12.             }
  13.             sum=max(sum,dp[s]);
  14.         }
  15.         return sum;
  16.     }
Add Comment
Please, Sign In to add comment