相同子序列集合

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3053

题意:给两段序列A和B,能找到多少(A' , B')两个集合相同,且A'为A 的子序列,B‘为B的子序列。

思路:暴力枚举A序列,将B序列的值用下标作标记。如果标记最大与最小差值加一等于a序列个数,就符合条件。

#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int MAX = 1e5 + 10;
typedef long long LL;
int a[100009];
int vis[100009];
int main()
{
    int n ;
    while(~scanf("%d" , &n) && n)
    {
       // memset(vis , 0 , sizeof(vis));
        for(int i = 1 ; i <= n ; i++)
        {
            scanf("%d" , &a[i]);
        }
        for(int i = 1 ; i <= n ; i++)
        {
            int t ;
            scanf("%d" , &t);
            vis[t] = i ;
        }
        int ans = 0 ;
        for(int i = 1 ; i <= n ; i++)
        {
            int max = vis[a[i]];
            int min = vis[a[i]];
            int num = 1 ;
            for(int j = i + 1 ; j <= n ; j++)
            {
                if(vis[a[j]] < min)
                    min = vis[a[j]];
                else if(vis[a[j]] > max)
                    max = vis[a[j]];
                num++;
                if(num == max - min + 1)
                    ans++ ;
            }
        }
        printf("%d
" , ans);
    }


    return 0;
}
原文地址:https://www.cnblogs.com/nonames/p/11311649.html