forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #339
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
...cess-String-with-Special-Operations-II/3614.Process-String-with-Special-Operations-II.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using ll = long long; | ||
class Solution { | ||
public: | ||
char processStr(string s, long long k) { | ||
k++; | ||
int n = s.size(); | ||
s = "#"+s; | ||
vector<ll>len(n+1); | ||
const ll INF = 1e15+5; | ||
|
||
for (int i=1; i<=n; i++) { | ||
char c = s[i]; | ||
ll prev = len[i-1]; | ||
ll now = prev; | ||
if ('a'<=c && c<='z') | ||
now = prev+1; | ||
else if (c == '*') | ||
now = prev>0 ? (prev-1): 0; | ||
else if (c=='#') | ||
now = prev*2; | ||
else if (c=='%') | ||
now = prev; | ||
len[i] = min(now, INF); | ||
} | ||
if (k==0 || k>(ll)len[n]) return '.'; | ||
|
||
for (int i=n; i>=1; i--) { | ||
char c = s[i]; | ||
ll before = len[i-1]; | ||
ll after = len[i]; | ||
if ('a'<=c && c<='z') { | ||
if (k==after) | ||
return c; | ||
} else if (c=='*') { | ||
|
||
} else if (c=='#') { | ||
if (k>before) | ||
k-=before; | ||
} else if ( c=='%') { | ||
k = before-k+1; | ||
} | ||
} | ||
|
||
return '.'; | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
Recursion/3614.Process-String-with-Special-Operations-II/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### 3614.Process-String-with-Special-Operations-II | ||
|
||
很显然,构造完全之后的字符串非常长,我们不可能在此基础上定位第k个元素。此题极大概率是递归反推。 | ||
|
||
我们分类思考每种操作下的第k个字符(注意是1-index)是什么情况: | ||
1. 添加一个字符,使得长度从a变成了a+1. 如果k=a+1,那么答案就是最新添加的字符。否则,递归求原字符串里的第k个。 | ||
2. 删减最后一个字符,使得长度从a变成了a-1. 这种情况下k不可能是a(否则无解),因此等效于递归求原字符串里的第k个。 | ||
3. 将字符串copy+append,使得长度从a变成了2a. 显然如果k<=a,递归求原字符串里的第k个。如果k>a,递归求原字符串里的第k-a个。 | ||
4. 将字符串反转,长度依然是k。显然,递归求原字符串里的第a+1-k个。 | ||
|
||
综上,只要知道每个回合的操作和长度变化,我们就可以把"求当前字符串的第k个元素",逆推为"求前一个回合字符串的第k'个元素",其中k'的计算如上。 | ||
|
||
注意,这个题可能无解。逆推的前提是正向的变化合法存在。所以我们必须先正向走一遍,记录下每个回合的字符串长度,最后检查k是否在最终长度的范围内。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.