Skip to content
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

7-InSange #28

Merged
merged 4 commits into from
May 1, 2024
Merged

7-InSange #28

merged 4 commits into from
May 1, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Apr 4, 2024

πŸ”— 문제 링크

λ¬Έμžμ—΄ 폭발

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

처음 μ ‘κ·Ό 방식

string s, boom;
int blen;

void Solve()
{
	cin >> s >> boom;

	blen = boom.length();
	stack<char> st;

	for (char c : s)
	{
		st.push(c);

		if (c == boom[blen-1] && st.size() >= blen)
		{
			bool flag = true;
			string s1 = "";
			for (int i = blen -1; i >= 0; i--)
			{
				if (boom[i] != st.top())
				{
					flag = false;
					break;
				}
				s1 = boom[i] + s1;
				st.pop();
			}

			if (flag == false)
			{
				for (int i = 0; i < s1.length(); i++)
				{
					st.push(s1[i]);
				}
			}
		}
	}

	string answer = "";
	while (!st.empty())
	{
		answer = st.top() + answer;
		st.pop();
	}

	if (answer == "") cout << "FRULA";
	else cout << answer;
}
  1. stack을 ν™œμš©ν•˜μ—¬ μž…λ ₯받은 단어 s(word)λ₯Ό ν•œκΈ€μž μ”© stack에 λ„£λŠ”λ‹€.
  2. 넣은 단어가 λ§Œμ•½ μž…λ ₯ 받은 단어 boom의 λ§ˆμ§€λ§‰ 단어와 μΌμΉ˜ν•  μ‹œμ— stack에 λ“€μ–΄κ°„ 단어듀과 λΉ„κ΅ν•œλ‹€. 단 stack에 λ“€μ–΄κ°„ 단어 길이(μ‚¬μ΄μ¦ˆ)κ°€ boomλ‹¨μ–΄μ˜ 길이보닀 짧을 μ‹œ μƒλž΅ν•œλ‹€.
  3. stack에 μ €μž₯λ˜μ–΄ μžˆλŠ” 단어λ₯Ό ν•˜λ‚˜μ”© κΊΌλ‚΄λ©΄μ„œ boomκ³Ό λΉ„κ΅ν•œλ‹€. κΊΌλ‚Έ λ‹¨μ–΄λŠ” μž„μ‹œ stack temp에 λ³΄κ΄€ν•œλ‹€.
  4. λ³΄κ΄€ν•˜λŠ” μ΄μœ λŠ” stackμ—μ„œ κΊΌλ‚Έ 단어가 boomκ³Ό μΌμΉ˜ν•˜μ§€ μ•Šμ„ 경우 λ‹€μ‹œ stack에 λ„£μ–΄μ£ΌκΈ° μœ„ν•¨μ΄λ‹€.
  5. μœ„μ˜ 과정을 μž…λ ₯받은 단어 s의 λ§ˆμ§€λ§‰ λ‹¨μ–΄κΉŒμ§€ λ°˜λ³΅ν•΄μ€€λ‹€.
  6. stack에 μŒ“μ—¬μžˆλŠ” 단어듀을 λ’€μ—μ„œ μ°¨μ°¨ λ„£μ–΄μ£Όλ©΄ 정닡이 λœλ‹€.

image
image
image
image
image
image
image
image
image
image

κ·ΈλŸ¬λ‚˜! λ¬Έμ œλŠ”
image
2초의 μ‹œκ°„μ œν•œ!!!
μ „μ²˜λ¦¬κΈ° λ§ˆλ‹€ μ‹œκ°„μ΄ λ‹€ λ‹€λ₯΄μ§€λ§Œ c++은 λŒ€λž΅ 1μ–΅λ²ˆμ˜ 연산을 μˆ˜ν–‰ν•˜λŠ”λ° 1초의 μ‹œκ°„μ΄ κ±Έλ¦°λ‹€κ³  ν•œλ‹€.
첫 번째 풀이 κ³Όμ •μœΌλ‘œ μ ‘κ·Όν•˜κ²Œ 되면 μ΅œμ•…μ˜ 경우 100만개의 문자λ₯Ό stack에 λ„£κ³  λΉΌκ³  λ‹€μ‹œ λ„£κ³ λ₯Ό λ°˜λ³΅ν•΄μ•Ό ν•œλ‹€.
stack의 ν¬κΈ°λŠ” boom의 길이보닀 컀야 μœ„μ˜ 과정을 거치기 λ•Œλ¬Έμ— 10만개의 λ‹¨μ–΄μ—μ„œ boom의 졜고 κΈΈμ΄λŠ” 36μ΄μ§€λ§Œ 40으둜 ν‰μΉ˜κ² λ‹€. 그럼 99960개의 λ‹¨μ–΄μ—μ„œ ν•œ λ²ˆμ”© λΉ„κ΅ν•˜λŠ” 과정을 거쳐야 ν•˜κ³  μ΅œμ΅œμ΅œμ΅œμ΅œμ•…μ˜ 경우 35번째 stack의 depthκΉŒμ§€ κ°”λ‹€κ°€ λ§ˆμ§€λ§‰ ν•œ 단어가 boom의 첫 번째 단어와 μΌμΉ˜ν•˜μ§€ μ•Šμ„κ²½μš° λ‹€μ‹œ stack에 λ„£μ–΄ μ£ΌλŠ” 경우λ₯Ό κ°μ•ˆν•˜μ—¬
36 * 35 * (1,000,000 - 40) =
image
의 크기가 λ‚˜μ˜€κ²Œ λœλ‹€..

λ‘λ²ˆμ§Έ 풀이 방법

stack을 μ‚¬μš©ν•˜κ²Œ 되면 boomκ³Ό λΉ„κ΅ν•˜κΈ° μœ„ν•΄μ„  ν•˜λ‚˜μ”© popν•˜μ—¬ μ°¨λ‘€μ°¨λ‘€ λΉ„κ΅ν•΄λ‚˜κ°€μ•Ό ν–ˆκΈ°μ— stack tempλ₯Ό ν•˜λ‚˜ μ„ μ–Έν•΄μ€˜μ„œ λ³΄κ΄€ν•΄μ£ΌλŠ” 방식을 μ‚¬μš©ν–ˆλ‹€.
κ·ΈλŸ¬λ‚˜ λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜κ²Œ 되면 맨 λ’€μ—μ„œ boom의 길이만큼만 비ꡐ해주면 되기 λ•Œλ¬Έμ— ꡳ이 pop을 ν•  ν•„μš”κ°€ 없기에 μœ„μ˜ 과정듀을 μƒλž΅ν•΄μ€„ 수 μžˆλ‹€.

  1. boomκ³Ό stack이 μΌμΉ˜ν•˜λ©΄ boom의 길이만큼 λ¬Έμžμ—΄μ˜ λ’€μ—μ„œ pop을 ν•΄μ£Όλ©΄ 되고
  2. μΌμΉ˜ν•˜μ§€ μ•ŠμœΌλ©΄ λ‹€μŒ 문자λ₯Ό λ¬Έμžμ—΄ stack뒀에 λΆ™μ—¬μ€€λ‹€.

μ΄λ ‡κ²Œ 되면 λ‹€μ‹œ stack에 pushλ₯Ό ν•΄μ£ΌλŠ” 과정이 μƒλž΅λ˜κΈ° λ•Œλ¬Έμ— λŒ€λž΅
36 * (1,000,000 - 40) = 35,998,560
만큼의 μ—°μ‚° μ‹œκ°„μ΄ 걸리게 λ˜λ©΄μ„œ 톡과할 수 μžˆλ‹€.

string s, boom;
int blen;

void Solve()
{
	cin >> s >> boom;

	blen = boom.length();
	
	string st = "";
	for (char c : s)
	{
		st.push_back(c);

		if (c == boom[blen-1] && st.size() >= blen)
		{
			bool flag = true;

			for (int i = 0; i < blen; i++)
			{
				if (boom[i] != st[st.length() - blen + i])
				{
					flag = false;
					break;
				}
			}

			if (flag)
			{
				for (int i = 0; i < blen; i++)
				{
					st.pop_back();
				}
			}
		}
	}

	if (st == "") cout << "FRULA";
	else cout << st;
}

ν›„κΈ°

μ„±μ›μ‹œμΉ˜κ°€ 푼것도 νŒŒμ΄μ¬μ—μ„œλŠ” μŠ€νƒμ΄λ‚˜ 큐 이런 것듀은 λͺ¨λ‘ 리슀트(λ°°μ—΄)둜 ν†΅ν•©λœλ‹€. 그렇기에 크기만큼 비ꡐ할 수 μžˆμ—ˆλ˜ κ²ƒμ²˜λŸΌ 우리 C++μ—μ„œλŠ” λ¬Έμžμ—΄ 자체λ₯Ό μŠ€νƒμœΌλ‘œ μΉ˜ν™˜ν•˜μ—¬ ν’€ 수 μžˆλ‹€.
정톡성은 C++, κ°„λž΅ν•¨μ€ python을 비ꡐ해 λ³Ό 수 μžˆμ—ˆλ˜ 것 κ°™λ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ—­μ‹œ νŒŒμ΄μ¬μ€ 신이야!

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char str[1000001];
char ans[1000001];
char boom[37];
char last;

int ptr, len;

void Explosion() {
    if (ptr <= len - 1) return;

    int pos = ptr - len;

    for (int i = 0; i < len; i++) 
        if (ans[pos + i] != boom[i]) return;
    
    ptr = pos;
}

int main() {
    cin.tie(nullptr);
	ios::sync_with_stdio(false);
    
    cin >> str >> boom;

    len = strlen(boom);
    last = boom[len - 1];
    ptr = 0;

    for (char c : str) {
        ans[ptr++] = c;
        if (c == last) Explosion();
    }

    ans[ptr] = '\0';

    if (!strlen(ans)) printf("FRULA\n");
    else printf("%s\n", ans);
}

image
μœ„κ°€ 제 μ½”λ“œ, μ•„λž˜κ°€ μ„ μƒλ‹˜ μ½”λ“œμž…λ‹ˆλ‹€. λ‚˜μ˜ 승리!
λ―Έλ¬˜ν•œ μ‹œκ°„μ°¨λŠ” stringλž‘ char[] μ°¨μ΄μΈκ±ΈκΉŒμš”?
λ©”λͺ¨λ¦¬ μ°¨μ΄λŠ” μ™œ 1.5λ°° 정도 차이가 λ‚ κΉŒμš”? char[1000001] κ°€ string보닀 클 거라고 μƒκ°ν–ˆλŠ”λ°.. ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ„œ λΆ„κΈ°λ₯Ό μ‘°μ ˆν•΄μ„œ κ·ΈλŸ°κ±ΈκΉŒμš”? 신기봉기

@yuyu0830
Copy link
Collaborator

yuyu0830 commented Apr 7, 2024

cin.tie(nullptr);
ios::sync_with_stdio(false);

이거 뢙이기 μ „μ—λŠ” 40ms μ˜€λŠ”λ° λΆ™μ΄λ‹ˆκΉŒ 8msκ°€ λμ–΄μš”! input λ°›λŠ” 속도도 차이가 ν¬λ‹€λŠ” κ±Έ λ‹€μ‹œ ν•œλ²ˆ 느끼고 κ°‘λ‹ˆλ‹€

@InSange
Copy link
Collaborator Author

InSange commented Apr 8, 2024

char str[1000001];
char ans[1000001];
char boom[37];
char last;

int ptr, len;

void Explosion() {
    if (ptr <= len - 1) return;

    int pos = ptr - len;

    for (int i = 0; i < len; i++) 
        if (ans[pos + i] != boom[i]) return;
    
    ptr = pos;
}

int main() {
    cin.tie(nullptr);
	ios::sync_with_stdio(false);
    
    cin >> str >> boom;

    len = strlen(boom);
    last = boom[len - 1];
    ptr = 0;

    for (char c : str) {
        ans[ptr++] = c;
        if (c == last) Explosion();
    }

    ans[ptr] = '\0';

    if (!strlen(ans)) printf("FRULA\n");
    else printf("%s\n", ans);
}

image μœ„κ°€ 제 μ½”λ“œ, μ•„λž˜κ°€ μ„ μƒλ‹˜ μ½”λ“œμž…λ‹ˆλ‹€. λ‚˜μ˜ 승리! λ―Έλ¬˜ν•œ μ‹œκ°„μ°¨λŠ” stringλž‘ char[] μ°¨μ΄μΈκ±ΈκΉŒμš”? λ©”λͺ¨λ¦¬ μ°¨μ΄λŠ” μ™œ 1.5λ°° 정도 차이가 λ‚ κΉŒμš”? char[1000001] κ°€ string보닀 클 거라고 μƒκ°ν–ˆλŠ”λ°.. ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ„œ λΆ„κΈ°λ₯Ό μ‘°μ ˆν•΄μ„œ κ·ΈλŸ°κ±ΈκΉŒμš”? 신기봉기

stdμ—μ„œ μ œκ³΅ν•˜λŠ” string은 μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ΄ μΆ”κ°€λ˜κ±°λ‚˜ λ³€κ²½λ˜μ—ˆμ„ λ•Œ λ°°μ—΄μ˜ 크기λ₯Ό λ‹€μ‹œ ν• λ‹Ήν•˜μ§€ μ•ŠκΈ° μœ„ν•΄ μΌλΆ€λŸ¬ μ—¬λΆ„ 곡간을 λ‚¨κ²¨λ‘”λ‹΅λ‹ˆλ‹€.
charν˜• λ°°μ—΄κ³Ό 차이점은 이것인 것 κ°™λ„€μš”.
비ꡐ λΈ”λ‘œκ·Έ

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

νŒŒμ΄μ¬μ—μ„œλŠ” 리슀트둜 μŠ€νƒμ˜ 역할을 λŒ€μ²΄ν–ˆλŠ”λ° c++에선 λ¬Έμžμ—΄μ„ μŠ€νƒμœΌλ‘œ μΉ˜ν™˜ν•΄μ„œ ν’€ 수 μžˆκ΅°μš”. 근데 μ†λ„μ˜ 차이가 .. μ—­μ‹œ c++ μ΄λ„€μš”

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μŠ€νƒμ„ μ“°μ§€μ•Šκ³  λ¬Έμžμ—΄μ„ μŠ€νƒμ²˜λŸΌ ν™œμš©ν•΄μ„œ ν’€ 수 μžˆλ‹€λ‹ˆ...되게 μ‹ κΈ°ν•˜λ„€μš”..! 더 κ³΅λΆ€ν•΄μ•Όκ² μŠ΅λ‹ˆλ‹€..

@InSange InSange merged commit 4cde274 into main May 1, 2024
@InSange InSange deleted the 7-InSange branch May 1, 2024 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants