11572

来源:互联网 发布:lol数据分析师 编辑:程序博客网 时间:2024/06/06 02:35

第二种

用一个map来存贮

#include <iostream>#include <map>#include <algorithm>using namespace std;#define maxn (int)(1e9)+1int t, n, x, ans, cnt, block;map<int, int> lastseen;int main() {    cin >> t;    while(t--) {cin >> n;lastseen.clear();ans = 0, cnt = 0, block = 0;//ans存储最大序列数,cnt对应当前序列数,block对应上一个重复元素坐标for(int i=1; i<=n; i++) {    cin >> x;    int lx = lastseen[x];    if(lx != 0) {          //如果此数已经存在block = max(block, lx); //当前重复数与上次重复书中最大的那个为现今重复数cnt = i-block-1;//计算去除重复后的新串数,-1是因为下一步的加一,防止多加    }    cnt++;//子串数加    lastseen[x] = i;//更新下标    ans = max(ans, cnt);//当前是否为大}cout << ans << '\n';    }}


0 0