康托爾對角線論證?我們真的能找到那條「獨特」的字串嗎?🤯
引言
想找出一堆字串入面的唯一字串?我天真地使用遞迴去暴力尋找,結果:蛤?怎麼時間複雜度比其他使用者高;_;
結果我發現還有更好的解決方法!那就是康托爾對角線論證。讓我們探討他的威力吧!
對日常生活及編程也沒有幫助。只是一個冷知識!如果你為了編程比賽,你可以看下去。
對角線論證是什麼?
它是一個在現實很難運用到的東西啦!!!來,我們玩一個遊戲!
你有沒辦法在不使用任何搜尋工具的情況下,找出沒有在下面的數組?這時候,對角線論證就能用上了。
101101000010101010101010101010
010100100001010100101010100101
101010010001001010101001010100
010100001010010101011010110101
101010000110101101011010010101
000000101010100111100111111010
110101010111110111011111111101
101011011101101110111111011110
101011001111011101111010010110
111101111111000000001000010001
100001000010010000011111100000
111111000011110000101110110101
101011011010101001001010010101
101001001010111010001000111111
101011010011100100001101010101
101011010011100100101010001111
101011010011100100001101011110
101011001010100000001101011101
111011101110111111111101011101
111100111111110011111111001111
101111010010110101111010010110
001001010010101001001010010101
101110110111011111101111011010
100101011101000100011111111010
100101010000000100101010000000
101011001111011101111010010111
101011001111011101111010010101
111100111111110011111111001101
111100110111110011111111001101
111100111111111011111111001101
解法(我將對角線附近改為*為方便查閱)
1*1101000010101010101010101010
*1*100100001010100101010100101
1*1*10010001001010101001010100
01*1*0001010010101011010110101
101*1*000110101101011010010101
0000*0*01010100111100111111010
11010*0*0111110111011111111101
101011*1*101101110111111011110
1010110*1*11011101111010010110
11110111*1*1000000001000010001
100001000*1*010000011111100000
1111110000*1*10000101110110101
10101101101*1*1001001010010101
101001001010*1*010001000111111
0111100111111*0*00111011010110
10101101001110*1*0101010001111
101011010011100*0*001101011110
1010110010101000*0*01101011101
11101110111011111*1*1101011101
111100111111110011*1*111001111
1011110100101101011*1*10010110
00100101001010100100*0*0010101
101110110111011111101*1*011010
1001010111010001000111*1*11010
10010101000000010010101*0*0000
101011001111011101111010*1*111
1010110011110111011110100*0*01
11111001111111010011101101*1*1
111110011111110100111011010*1*
1111100111111101001110110101*1
💡 對角線方法的關鍵步驟
1️⃣ 提取對角線上的數字(即第 i 行的第 i 個位元)
111110011111110100111011010111
2️⃣ 翻轉每個位元(0 → 1,1 → 0)
000001100000001011000100101000
3️⃣ 新產生的二進制字串,必然與原列表中的每一行都不同!
👉 這就是為什麼我們總能找到一個「不在列表中的獨特數組」!😎
為何一定要轉換?
如果你有注意,對角線其實有與上面30組裡面其中1組是相同。這就是為何一定要轉換,原理其實在每組數字裡選擇1個數字來轉換。這亦就做成這個原理的缺陷,必須附合以下條件才能使用。
數組數量\(\le\)數組長度
為何我使用極端的例子?
因為在CodeForce或Leetcode裡,往往也有很極端的找唯一字串的題目。這樣可以令你對對角線論證更深的記憶。>w<
結語
我學了一個集合論的東西啊OAO!! 但只對編程比賽有幫助,就沒任何作為了!! 康托爾!!給我從棺材裡出來!QAQ
Cantor's Diagonal Argument: Can We Really Find That "Unique" String? 🤯🍺
Introduction
Trying to find a unique string among a bunch of binary strings?
I naively used brute force to search, and the result was: huh? 🤨 Why is my time complexity higher than everyone else's? ;_;
Then, I discovered a better solution! That is Cantor's Diagonal Argument. Let's explore its power! 🚀
In daily life and coding, this theory is useless. It's just an interesting piece of knowledge! But if you're into competitive programming, you might want to keep reading.
What is the Diagonal Argument?
This is something that's very difficult to apply in real life!!! But hey, let's play a game! 🎮
Can you, without using any search tools, find a number that is not in the list below? 🤔
Try using the diagonal method and see if you can figure it out yourself!
101101000010101010101010101010
010100100001010100101010100101
101010010001001010101001010100
010100001010010101011010110101
101010000110101101011010010101
000000101010100111100111111010
110101010111110111011111111101
101011011101101110111111011110
101011001111011101111010010110
111101111111000000001000010001
100001000010010000011111100000
111111000011110000101110110101
101011011010101001001010010101
101001001010111010001000111111
101011010011100100001101010101
101011010011100100101010001111
101011010011100100001101011110
101011001010100000001101011101
111011101110111111111101011101
111100111111110011111111001111
101111010010110101111010010110
001001010010101001001010010101
101110110111011111101111011010
100101011101000100011111111010
100101010000000100101010000000
101011001111011101111010010111
101011001111011101111010010101
111100111111110011111111001101
111100110111110011111111001101
111100111111111011111111001101
Solution (I've marked the diagonal elements with * for easy reference)
1*1101000010101010101010101010
*1*100100001010100101010100101
1*1*10010001001010101001010100
01*1*0001010010101011010110101
101*1*000110101101011010010101
0000*0*01010100111100111111010
11010*0*0111110111011111111101
101011*1*101101110111111011110
1010110*1*11011101111010010110
11110111*1*1000000001000010001
100001000*1*010000011111100000
1111110000*1*10000101110110101
10101101101*1*1001001010010101
101001001010*1*010001000111111
0111100111111*0*00111011010110
10101101001110*1*0101010001111
101011010011100*0*001101011110
1010110010101000*0*01101011101
11101110111011111*1*1101011101
111100111111110011*1*111001111
1011110100101101011*1*10010110
00100101001010100100*0*0010101
101110110111011111101*1*011010
1001010111010001000111*1*11010
10010101000000010010101*0*0000
101011001111011101111010*1*111
1010110011110111011110100*0*01
11111001111111010011101101*1*1
111110011111110100111011010*1*
1111100111111101001110110101*1
💡 Key Steps in the Diagonal Method
1️⃣ Extract the diagonal elements (i.e., the i-th bit from the i-th row).
111110011111110100111011010111
2️⃣ Flip each bit (0 → 1, 1 → 0).
000001100000001011000100101000
3️⃣ The new binary string must be different from every row in the original list!
And that's why we can always find a unique number that isn't in the list! 😎
Why Do We Need to Flip the Bits?
If you look carefully, you’ll notice that one of the diagonal sequences already exists in the list. This is why we must flip the bits—otherwise, we might accidentally end up with a duplicate. For this method to work, the number of sequences must be ≤ the length of each sequence.
Why Did I Use an Extreme Example?
Because in Codeforces or LeetCode, there are often very extreme problems where you need to find the only unique binary string.This method ensures you'll never forget Cantor's Diagonal Argument! >w<
Conclusion
I learned a concept from set theory OAO!! But except for being useful in competitive programming, it has no practical use at all!! Cantor!! Get out of your grave and explain this to me! QAQ