UVA - 11572
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                UVA - 11572
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                題意:輸入一個長度為n(n)的序列A,找到一個盡量長的連續子序列,使得序列中沒有相同元素。
分析:兩種方法,一是以序列為對象搜索,每次向前增加一個,有重復就減少。
二是,找到相同的元素位置,找出相同元素之間距離最大的。
#include<iostream> #include<string> #include<sstream> #include<set> #include<algorithm> #include<vector> using namespace std; const int maxn = 1000000 + 100; int A[maxn];int main() {int kase;int n;cin >> kase;while (kase--) {cin >> n;for (int i = 0; i < n; i++) {cin >> A[i];}set<int>s;int L = 0, R = 0, ans = 0;while (R < n) {while (R<n&&!s.count(A[R]))s.insert(A[R++]);ans = max(ans, R - L);s.erase(A[L++]);}cout << ans << endl;}return 0; } #include<iostream> #include<string> #include<sstream> #include<set> #include<algorithm> #include<vector> #include<map> using namespace std; const int maxn = 1000000 + 100; int A[maxn]; int last[maxn]; int main() {int kase;int n;map<int, int>mp;cin >> kase;while (kase--) {mp.clear();cin >> n;for (int i = 0; i < n; i++) {cin >> A[i];if (!mp.count(A[i]))last[i] = -1;else last[i] = mp[A[i]];mp[A[i]] = i;//更新位置}int L = 0, R = 0, ans = 0;while (R < n) {//找出長度最大的while (R < n&&last[R] < L)R++;ans = max(ans, R - L);L++;}cout << ans << endl;}return 0; }?
總結
以上是生活随笔為你收集整理的UVA - 11572的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: UVA - 1606 Amphiphil
- 下一篇: uva-1471
