Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ class Solution {
return good*1.0/all;
}

void dfs(int level, vector<int>&set1, vector<int>&set2)
void dfs(int k, vector<int>&set1, vector<int>&set2)
{
if (level == balls.size())
{
int tot1 = accumulate(set1.begin(), set1.end(), 0);
int tot2 = accumulate(set2.begin(), set2.end(), 0);
if (tot1!=tot2) return;
if (k == balls.size())
{
if (accumulate(set1.begin(), set1.end(), 0) != accumulate(set2.begin(), set2.end(), 0))
return;

long long p = 1;
for (int i=0; i<balls.size(); i++)
p *= C[balls[i]][set1[i]];
for (int i=0; i<balls.size(); i++)
p *= C[balls[i]-set1[i]][set2[i]];

all += p;

Expand All @@ -49,13 +46,13 @@ class Solution {
return ;
}

for (int i=0; i<=balls[level]; i++)
for (int i=0; i<=balls[k]; i++)
{
set1[level] += i;
set2[level] += balls[level]-i;
dfs(level+1, set1, set2);
set1[level] -= i;
set2[level] -= balls[level]-i;
set1[k] += i;
set2[k] += balls[k]-i;
dfs(k+1, set1, set2);
set1[k] -= i;
set2[k] -= balls[k]-i;
}
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ C(i,j) = C(i-1, j-1) + C(i-1, j)
}
}
```

本题的时间DFS的复杂度是`6^8 = 1679616`,可以接受,
4 changes: 2 additions & 2 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,7 @@
[388.Longest-Absolute-File-Path](https://github.com/wisdompeak/LeetCode/tree/master/String/388.Longest-Absolute-File-Path) (M+)
[418.Sentence-Screen-Fitting](https://github.com/wisdompeak/LeetCode/tree/master/String/418.Sentence-Screen-Fitting) (M+)
[423.Reconstruct-Original-Digits-from-English](https://github.com/wisdompeak/LeetCode/tree/master/Others/423.Reconstruct-Original-Digits-from-English) (H-)
[527.Word-Abbreviation](https://github.com/wisdompeak/LeetCode/tree/master/String/527.Word-Abbreviation) (M+)
[556.Next Greater Element III](https://github.com/wisdompeak/LeetCode/tree/master/String/556.Next-Greater-Element-III) (H-)
[556.Next-Greater-Element-III](https://github.com/wisdompeak/LeetCode/tree/master/String/556.Next-Greater-Element-III) (H-)
616.Add-Bold-Tag-in-String (M)
[467.Unique-Substrings-in-Wraparound-String](https://github.com/wisdompeak/LeetCode/tree/master/String/467.Unique-Substrings-in-Wraparound-String) (H-)
[564.Find-the-Closest-Palindrome](https://github.com/wisdompeak/LeetCode/tree/master/String/564.Find-the-Closest-Palindrome) (H)
Expand All @@ -878,6 +877,7 @@
* ``Abbreviation``
[408.Valid-Word-Abbreviation](https://github.com/wisdompeak/LeetCode/tree/master/String/408.Valid-Word-Abbreviation) (M)
[411.Minimum-Unique-Word-Abbreviation](https://github.com/wisdompeak/LeetCode/tree/master/String/411.Minimum-Unique-Word-Abbreviation) (H)
[527.Word-Abbreviation](https://github.com/wisdompeak/LeetCode/tree/master/String/527.Word-Abbreviation) (M+)
[2060.Check-if-an-Original-String-Exists-Given-Two-Encoded-Strings](https://github.com/wisdompeak/LeetCode/tree/master/Recursion/2060.Check-if-an-Original-String-Exists-Given-Two-Encoded-Strings) (H)
* ``Rolling Hash``
[1044.Longest-Duplicate-Substring](https://github.com/wisdompeak/LeetCode/tree/master/String/1044.Longest-Duplicate-Substring) (H)
Expand Down
63 changes: 0 additions & 63 deletions String/527.Word-Abbreviation/527.Word Abbreviation.cpp
View file Open in desktop

This file was deleted.

56 changes: 56 additions & 0 deletions String/527.Word-Abbreviation/527.Word-Abbreviation.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution {
public:
vector<string> wordsAbbreviation(vector<string>& words)
{
int n = words.size();
vector<string>rets(n);

vector<int>Set;
for (int i=0; i<n; i++)
Set.push_back(i);

int abbrNum = 1;
while (1)
{
unordered_map<string, vector<int>> Map;

for (int idx: Set)
{
string abbr = getAbbr(words[idx], abbrNum);
Map[abbr].push_back(idx);
}
Set.clear();

for (auto& [abbr, indices]: Map)
{
if (indices.size() > 1)
{
for (int idx: indices)
Set.push_back(idx);
}
else
rets[indices[0]] = abbr;
}

abbrNum += 1;
if (Set.size() == 0)
break;
}

return rets;
}

string getAbbr(string s, int abbrNum)
{
if (s.size() < 3) return s;

string t;
t = s.substr(0, abbrNum);
t += to_string(s.size() - abbrNum - 1);
t += s.back();

if (t.size() == s.size()) return s;

return t;
}
};

AltStyle によって変換されたページ (->オリジナル) /