uva10635 Prince and Princess
阿新 • • 發佈:2019-05-10
space oid cstring str 包含 lse ces mem lcs
給你兩條路徑,對於每條路徑上的點各不相同,請你求出兩條路徑最長公共部分的長度。
Input
第一行是數據組數t 每組數據的第一行包含三個數,n,p,q。其中路徑上的點的大小不會超過n^2. 第二行包含p+1個數,表示第一條路徑 第三行包含q+1個數,表示第二條路徑。
Output
輸出最長路徑長度。
沖上去寫了個LCS,死在250^2的數據範圍之下
正解是LIS
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespaceView Codestd; 5 const int maxn=250*250+2; 6 const int INF=1e9+5; 7 int t,n,q,p,x,tot,cas,a[maxn],b[maxn],c[maxn],d[maxn]; 8 template <class t>void red(t &x) 9 { 10 x=0; 11 int w=1; 12 char ch=getchar(); 13 while(ch<‘0‘||ch>‘9‘) 14 { 15 if(ch==‘-‘) 16 w=-1; 17 ch=getchar(); 18 } 19 while(ch>=‘0‘&&ch<=‘9‘) 20 { 21 x=(x<<3)+(x<<1)+ch-‘0‘; 22 ch=getchar(); 23 } 24 x*=w; 25 } 26 void input() 27 { 28 freopen("input.txt","r",stdin); 29 //freopen("output.txt","w",stdout); 30} 31 int main() 32 { 33 //input(); 34 red(t); 35 while(t--) 36 { 37 ++cas; 38 printf("Case %d: ",cas); 39 red(n); 40 red(p); 41 red(q); 42 memset(c,0,sizeof(c)); 43 memset(d,0x3f,sizeof(d)); 44 for(int i=1;i<=p+1;++i) 45 { 46 red(a[i]); 47 c[a[i]]=i; 48 } 49 for(int i=1;i<=q+1;++i) 50 { 51 red(x); 52 b[i]=c[x]; 53 } 54 tot=0; 55 d[++tot]=b[1]; 56 for(int i=2;i<=q+1;++i) 57 { 58 if(!b[i]) 59 continue; 60 if(b[i]>d[tot]) 61 d[++tot]=b[i]; 62 else 63 { 64 int j=lower_bound(d+1,d+tot+1,b[i])-d; 65 d[j]=b[i]; 66 } 67 } 68 printf("%d\n",tot); 69 } 70 return 0; 71 }
uva10635 Prince and Princess