UVA - 1592:Database

題目的意思是找到兩行在兩列處相等,主要要做的是記錄某個值是否重復出現過。

經過思考,我的思路是:每一列用一個unordered_map<string,vector<int>>記錄單詞出現的行數,對于某一行中的兩列,如果有兩個元素在同一其他行出現了重復,則可以輸出結果

例如,第4行的第1個元素在1 3 5 6行都出現了重復,第4行的第2個元素在2 3行出現了重復,則r1 = 3; r2 = 4; c1 = 1; c2 =2;

每一行用一個數據結構保存每一列在哪些行出現了重復,如果某一行被重復了兩次,則說明不滿足PNF,輸出結果。

為了保存每一行中出現重復的列,我們可以使用兩種數據結構,一種是數組,一種是unordered_map,保存的鍵值對<key, val>的含義是,本行的val列在key行出現了重復。

數組每一行需要清空,復雜度是O(n),鍵值對的插入的復雜度是O(n)(每一次是常數,最多插入n次),總復雜度是O(n^2)

使用unordered_map的復雜度每一行也是O(n),但是常數會更大一些。

讀入可以一個一個讀,如果嫌棄復雜可以將所有的,變成\n然后一行一行讀,我使用的是C++的正則表達式庫regex,挺好用的。

兩種數據結構的實現代碼如下:
數組

#include <iostream>
#include <unordered_map>
#include <array>
#include <vector>
#include <regex>
#include <string>using namespace std;namespace {constexpr int MAXN = 1e4 + 5;constexpr int MAXM = 1e1 + 5;array<int, MAXN> exists;array<unordered_map<string, vector<int>>, MAXM> dict;int n, m;const string YES = "YES";const string NO = "NO";string line, word;regex r("([^,]*),");bool flag;int r1, r2, c1, c2;
}int main() {while (cin >> n >> m) {fill(dict.begin(), dict.end(), unordered_map<string, vector<int>>());//讀取空行getline(cin, line);flag = false;for (int i = 1; i <= n; ++i) {getline(cin, line);if (flag) continue;fill(exists.begin(), exists.begin() + i, 0);line.push_back(',');int j = 1;for (sregex_iterator it(line.begin(), line.end(), r), end_it; it != end_it; ++it, ++j) {word = it->str(1);
//                cout << word << " ";auto &exist = dict[j][word];for (auto k : exist) {if (exists[k] > 0) {r1 = k; r2 = i;c1 = exists[k]; c2 = j;flag = true;break;} else {exists[k] = j;}}if (flag) break;dict[j][word].push_back(i);}
//            cout << "\n";}if (flag) {cout << NO << "\n"<< r1 << " " << r2 << "\n"<< c1 << " " << c2 << "\n";} else {cout << YES << "\n";}}
}

unordered_map

#include <iostream>
#include <unordered_map>
#include <array>
#include <vector>
#include <regex>
#include <string>
#include <set>using namespace std;namespace {constexpr int MAXN = 1e4 + 5;constexpr int MAXM = 1e1 + 5;unordered_map<int, int> exists;array<unordered_map<string, vector<int>>, MAXM> dict;int n, m;const string YES = "YES";const string NO = "NO";string line, word;regex r("([^,]*),");bool flag;int r1, r2, c1, c2;
}int main() {while (cin >> n >> m) {fill(dict.begin(), dict.end(), unordered_map<string, vector<int>>());//讀取空行getline(cin, line);flag = false;for (int i = 1; i <= n; ++i) {getline(cin, line);if (flag) continue;exists.clear();line.push_back(',');int j = 1;for (sregex_iterator it(line.begin(), line.end(), r), end_it; it != end_it; ++it, ++j) {word = it->str(1);
//                cout << word << " ";auto &exist = dict[j][word];for (auto k : exist) {if (exists.count(k) > 0) {r1 = k; r2 = i;c1 = exists[k]; c2 = j;flag = true;break;} else {exists[k] = j;}}if (flag) break;dict[j][word].push_back(i);}
//            cout << "\n";}if (flag) {cout << NO << "\n"<< r1 << " " << r2 << "\n"<< c1 << " " << c2 << "\n";} else {cout << YES << "\n";}}
}

提交以后發現,還是使用數組快一點,畢竟常數比較小

看了書以后發現書中采用的是完全不同的思路,不過把字符串首先哈希成數字這個思想我覺得挺重要,對上面的代碼也是一種優化。以后遇到對這種復雜數據結構的映射處理都應該想到這種哈希方法。

然后遍歷每兩列,將每一行的兩個的值加入unordered_map,如果出現重復則輸出。

復雜度是O(nm^2),按道理來講比上面的代碼會快很多,但是實際提交卻比上面的還慢,不清楚為什么,覺得OJ的測評也挺奇怪的。

經過思考,我覺得上面O(n^2)的算法中,每一行的復雜度在實際數據中應該比O(n)小很多,導致算法大概就是O(n)的,所以速度更快。

#include <iostream>
#include <unordered_map>
#include <array>
#include <vector>
#include <regex>
#include <string>
#include <set>using namespace std;namespace {constexpr int MAXN = 1e4 + 5;constexpr int MAXM = 1e1 + 5;array<array<int, MAXM>, MAXN> dict;class StrHash {unordered_map<string, int> hash;int idx = 0;public:int operator ()(const string &s);};int StrHash::operator()(const string &s) {if (hash.count(s)) return hash[s];return hash[s] = idx++;}class IntHash {int len = 0;public:IntHash(int _len):len(_len) {}int operator() (int a, int b) {return a * len + b;}};int n, m, len;regex r("([^,]*),");string line;bool flag;int r1, r2, c1, c2;unordered_map<int, int> record;
}int main() {ios::sync_with_stdio(false);while (cin >> n >> m) {flag = false;IntHash intHash(n * m);StrHash strHash;getline(cin, line);for (int i = 0; i < n; ++i) {getline(cin, line);line.push_back(',');int j = 0;for (sregex_iterator it(line.begin(), line.end(), r), end_it; it != end_it; ++it, ++j) {
//                cout << it->str(1) << endl;dict[i][j] = strHash(it->str(1));}}int word;for (int i = 0; i < m; ++i) {for (int j = 0; j < i; ++j) {record.clear();for (int k = 0; k < n; ++k) {word = intHash(dict[k][i], dict[k][j]);if (record.count(word) > 0) {flag = true;r1 = record[word] + 1;r2 = k + 1;c1 = j + 1;c2 = i + 1;break;} else {record[word] = k;}}if (flag) break;}if (flag) break;}if (flag) {cout << "NO\n"<< r1 << " " << r2 << "\n"<< c1 << " " << c2 << "\n";} else {cout << "YES\n";
//}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383531.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383531.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383531.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++ array初始化需要雙層大括號

對于array的初始化我們可以使用列表初始化&#xff1a; array<int, 8> test {1,2,3,4,5,6,7,8 };但是當我們不再使用簡單的內置類型array時&#xff1a; array<pair<int, int>, 8> dirs {{-1, -1},{-1, 0},{-1, 1},{0, -1},{0, 1},{1, -1},{1, 0},{1, 1}…

Qt for Android環境配置

最近想寫一個小APP&#xff0c;但是又不想用Android Studio進行開發&#xff0c;想要用C進行開發&#xff0c;聽說Qt可以進行Android開發&#xff0c;就想嘗試一下&#xff0c;結果花了一天時間來配置環境。。。而且發現windows下配置環境更簡單一些&#xff08;我中途還切換到…

UVa-12333:Revenge of Fibonacci 高精度

之前自己仿照紫書上寫了高精度庫&#xff0c;完善了乘法、減法&#xff0c;并且通過了和C高精度庫GMP的對拍測試&#xff0c;并在一些OJ上過了一些高精度的模板題&#xff0c;代碼倉庫地址&#xff1a;https://github.com/Edward-Elric233/BigInt 求解思路 題目的意思是求前1…

vim命令筆記

vim折疊函數&#xff1a;https://www.cnblogs.com/zlcxbb/p/6442092.html Vim錄制宏及使用&#xff1a;https://www.jianshu.com/p/9d999c72a9f3 將vim與系統剪貼板的交互使用&#xff1a;https://zhuanlan.zhihu.com/p/73984381

Educational Codeforces Round 114總結

緒論 https://codeforces.com/contest/1574/ 以前想要打CF&#xff0c;總是覺得沒有時間&#xff0c;要做這個&#xff0c;要做那個&#xff0c;現在時間充裕了一些&#xff0c;想要多打一些CF&#xff0c;但是光打比賽不總結是沒有什么幫助的&#xff0c;這是我從以前的ACM訓…

UVA - 210:Concurrency Simulator

題目鏈接&#xff1a;https://vjudge.net/problem/UVA-210 題目分析 就是一道模擬題&#xff0c;但是細節有點多。 寫代碼兩個小時&#xff0c;調試代碼用了兩天。。。很長時間不刷題了&#xff0c;這道雖然算法簡單但是細節滿滿的題目對我來說是一個很好的熱身。 盡量不要去…

UVA - 514:Rails

題目鏈接&#xff1a;https://vjudge.net/problem/UVA-514 題目分析 題目的意思是給一個棧輸入一系列數據&#xff0c;在這個過程中可以出棧&#xff0c;看能否達到某個結果。 剛開始我覺得這個情況好多&#xff0c;因此不是用模擬&#xff0c;而應該觀察結果本身。對于結果中…

UVA - 442:Matrix Chain Multiplication

題目鏈接&#xff1a;https://vjudge.net/problem/UVA-442 題目分析 題目的意思非常簡單&#xff0c;就是給定一個矩陣乘法的表達式然后計算就可以了。隨便寫寫 AC代碼 #include <iostream> #include <deque> #include <vector> #include <string>…

leetcode869. 重新排序得到 2 的冪

題目連接&#xff1a;https://leetcode-cn.com/problems/reordered-power-of-2/ 題目分析 如果直接順著題目的思路&#xff0c;得到數字n的全排列&#xff0c;然后再去判斷其是不是2的冪是比較復雜的。 我們應該注意到&#xff0c;因為數字是可以隨意排列的&#xff0c;因此所…

使用wireshark+ssh+tcpdump遠程抓包

因為需要抓取遠程服務器上的數據包&#xff0c;又不想使用tcpdump這種命令行工具進行&#xff08;用了wireshark后誰還愿意去看密密麻麻的命令行呢&#xff09;&#xff0c;所以在網上查找了一下使用wireshark遠程抓包的方法&#xff0c;在這里記錄一下。 原生支持 wireshark…

C++ Variadic Templates(可變參數模板)

本文參考侯捷老師的視頻&#xff1a;https://www.youtube.com/watch?vTJIb9TGfDIw&listPL-X74YXt4LVYo_bk-jHMV5T3LHRYRbZoH 以及C primer第五版 相關內容。 可變參數模板函數 //遞歸的終止條件 void print() {} //Variadic Templates //一般用于遞歸處理 template <…

Ubuntu修復Fix Busybox Initramfs錯誤

今天早上我打開電腦&#xff0c;進入Ubuntu系統&#xff0c;結果黑屏了&#xff0c;屏幕顯示&#xff1a; BusyBox v1.30.1 (Ubuntu 1:1.30.1-4ubuntu6.1) built-in shell (ash) Enter help for a list of built-in commands.(initramfs)然而我并不知道這個是什么意思&#x…

Leetcode第284場周賽

緒論 最近發現Leetcode每周的周賽難度挺適合我的&#xff0c;而且時間也比較友好&#xff08;不像Codeforces每次都是半夜&#xff09;。所以連續參加了三周的周賽。這次才想起來應該記錄一下自己的參賽歷程。一方面是總結經驗&#xff0c;另一方面有了記錄就更有動力去提升&a…

Leetcode第286場周賽

緒論 上周因為有事沒有參加周賽&#xff0c;這周沒有錯過。這次周賽拿到了人生第一個AK&#xff0c;參加大大小小的比賽這么多次&#xff0c;從來沒有AK過&#xff0c;淚目了。 感覺這次比賽的思維難度對我來講稍高一些&#xff0c;前三道題就花了一個小時&#xff0c;而以往…

第287場周賽

緒論 雖然是上周日參加的比賽&#xff0c;但是這周沒有怎么學習&#xff0c;每天就是玩耍。也導致對周賽的總結遲遲沒有進行。想著再拖下去下次周賽都要開始了&#xff0c;在這里補一下。 這場比賽總體比上場簡單一些&#xff0c;但是最后一道題因為忘記初始化類內變量導致調試…

第288場周賽

緒論 雖然沒有AK&#xff0c;但是不知道為什么排名比以前AK了都靠前。可能是因為最后一道題有些難度&#xff0c;縮小了我和大佬之間的差距。最后一個小時寫最后一道題&#xff0c;累死累活想了一個貪心遍歷的算法&#xff0c;當時是一直RE&#xff0c;后來下來調了調又WA了。 …

Clion遠程部署和運行

緒論 作為Clion的忠實粉絲&#xff0c;現在的我的幾乎所有的coding都是通過Clion完成。因為需要在服務器上進行開發&#xff0c;又離不開Clion&#xff0c;就了解了如何通過Clion遠程部署和開發。 主要是借鑒了博客&#xff1a;使用Clion優雅的完全遠程自動同步和遠程調試c。如…

C++ 單例模式 call_once : terminate called after throwing an instance of ‘std::system_error‘

在學習了C中可以使用call_once進行初始化資源后&#xff0c;我就想著寫一個單例模板供以后使用。 template<typename T> class SingleTon {using Ptr std::shared_ptr<T>;static Ptr p;static std::once_flag flag;template<typename ...Args>static void …

C++讀寫鎖造成死鎖

C14支持std::shared_timed_mutex C17支持std::shared_mutex 前者相比后者支持的操作更多&#xff0c;但是后者相對性能更好。 使用std::lock_guard<std::shared_mutex>和std::unique_lock<std::shared_mutex>互斥訪問使用std::shared_lock<std::shared_mutex…

每日一題:449. 序列化和反序列化二叉搜索樹

題目分析 題目鏈接&#xff1a;449. 序列化和反序列化二叉搜索樹 覺得序列化很簡單&#xff0c;前序遍歷、后序遍歷、中序遍歷、層序遍歷等等。其中得到前序遍歷和后序遍歷是可以通過遞歸解法反序列化的&#xff0c;覺得這樣子做有點復雜。就想著可不可以一次遍歷。一次遍歷的…