From 805a6cf7c583756862d53b5b6dfd698937a003f9 Mon Sep 17 00:00:00 2001 From: lib Date: Sat, 6 Mar 2021 22:55:29 +0800 Subject: [PATCH] Question: https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3662/ --- Short-Encoding-Words/main.cpp | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Short-Encoding-Words/main.cpp diff --git a/Short-Encoding-Words/main.cpp b/Short-Encoding-Words/main.cpp new file mode 100644 index 0000000..753de9c --- /dev/null +++ b/Short-Encoding-Words/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +using namespace std; + +/** + * @see https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3662/ + */ +class Solution { +public: + int minimumLengthEncoding(vector& words) { + int ans = 0; + unordered_set us(words.begin(), words.end()); + for (string word : us) { + for(int i = 1; i < word.size(); ++i) { + us.erase(word.substr(i)); + } + } + for (string word : us) ans += word.size() + 1; + return ans; + } +}; + +template +vector write_vector(vector &v, string &temp, istringstream &ss) { + while (ss >> temp) + v.push_back(temp); + return v; +} + +int main() { + string line; + vector words; + string temp; + getline(cin, line); + istringstream ss(line); + words = write_vector(words, temp, ss); + Solution sol; + int r = sol.minimumLengthEncoding(words); + cout << r << endl; + + return 0; +} \ No newline at end of file