LA-5052 (暴力)

题意:

给[1,n]的两个排列,统计有多少个二元组(a,b)满足a是A的连续子序列,b是B的连续子序列,a,b中包含的数相同;

思路:

由于是连续的序列,且长度相同,可以枚举一个串的子串,找出这个子串在另一个串的最小位置和最大位置,看之间相距的长度是否是子串的长度;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e7+10;
const int maxn=3e3+10;
const double eps=1e-6;

int n;
int b[maxn],l[maxn][maxn],r[maxn][maxn],p[maxn];
int main()
{
      while(1)
      {
        read(n);
        if(n==0)break;
        int a;
        For(i,1,n)read(a),p[a]=i;
        For(i,1,n)read(b[i]);
        int ans=0;
        For(i,1,n)
        {
            l[i][i]=r[i][i]=p[b[i]];
            For(j,i+1,n)
            {
                l[i][j]=min(l[i][j-1],p[b[j]]);
                r[i][j]=max(r[i][j-1],p[b[j]]);
                if(r[i][j]-l[i][j]==j-i)ans++;
            }
        }
        cout<<ans<<"
";
      }  
        
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5681862.html