【2015上海赛区网络赛】 hdu 5469 Antonidas
Antonidas
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 371 Accepted Submission(s): 89
Problem Description
Given a tree with N vertices and N−1 edges. Each vertex has a single letter Ci. Given a string S, you are to choose two vertices A and B, and make sure the letters catenated on the shortest path from A to B is exactly S. Now, would you mind telling me whether the path exists?
Input
The first line is an integer T, the number of test cases.
For each case, the first line is an integer N. Following N−1 lines contains two integers a and b, meaning there is an edge connect vertex a and vertex b.
Next line contains a string C, the length of C is exactly N. String C represents the letter on each vertex.
Next line contains a string S.
1≤T≤200, 1≤N≤104, 1≤a,b≤N, a≠b, |C|=N, 1≤|S|≤104. String C and S both only contain lower case letters.
For each case, the first line is an integer N. Following N−1 lines contains two integers a and b, meaning there is an edge connect vertex a and vertex b.
Next line contains a string C, the length of C is exactly N. String C represents the letter on each vertex.
Next line contains a string S.
1≤T≤200, 1≤N≤104, 1≤a,b≤N, a≠b, |C|=N, 1≤|S|≤104. String C and S both only contain lower case letters.
Output
First, please output "Case #k: ", k is the number of test case. See sample output for more detail.
If the path exists, please output “Find”. Otherwise, please output “Impossible”.
If the path exists, please output “Find”. Otherwise, please output “Impossible”.
Sample Input
2
7
1 2
2 3
2 4
1 5
5 6
6 7
abcdefg
dbaefg
5
1 2
2 3
2 4
4 5
abcxy
yxbac
Sample Output
Case #1: Find
Case #2: Impossible
Source
代码:(DPS+字符匹配)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include <string.h> 5 #define MAX 1000100 6 using namespace std; 7 struct edge{int to,next;}edg[2*MAX]; 8 struct CCCC{int to,next;}CCC[2*MAX]; 9 int First_C[MAX]; 10 int sign_C; 11 char ID[MAX]; 12 int first[MAX]; 13 int sign; 14 int Stack[MAX];/*堆栈*/ 15 char Str[MAX]; 16 int OK; 17 void Cread(int N) 18 { 19 for(int i=0;i<=N;i++) 20 { 21 first[i]=0; 22 }sign=1; 23 } 24 void add_e(int x,int y) 25 { 26 edg[sign].to=y; 27 edg[sign].next=first[x]; 28 first[x]=sign++; 29 } 30 void add_c(int x,int y) 31 { 32 CCC[sign_C].to=y; 33 CCC[sign_C].next=First_C[x]; 34 First_C[x]=sign_C++; 35 } 36 void DFS(int n,int nn,int d,int Len) 37 { 38 if(OK)return; 39 if(d==Len){OK=1;return ;} 40 for(int i=first[n];i;i=edg[i].next) 41 { 42 int TMD=edg[i].to; 43 if(TMD==nn)continue; 44 if(Str[d]==ID[TMD]) 45 DFS(TMD,n,d+1,Len); 46 } 47 return ; 48 } 49 50 int main() 51 { 52 int n,m,i,j,a,b,T,t=1; 53 scanf("%d",&T); 54 while(T--) 55 { 56 scanf("%d",&n); 57 m=n-1; 58 Cread(n); 59 for(i=0;i<=30;i++)First_C[i]=0;sign_C=1; 60 for(i=0;i<m;i++) 61 { 62 scanf("%d%d",&a,&b); 63 add_e(a,b);add_e(b,a); 64 } 65 scanf("%s",ID+1); 66 for(i=1;i<=n;i++)add_c(ID[i]-'a',i); 67 scanf("%s",Str); 68 int Len=strlen(Str); 69 OK=0; 70 for(i=First_C[Str[0]-'a'];i;i=CCC[i].next) 71 { 72 DFS(CCC[i].to,0,1,Len); 73 if(OK)break; 74 } 75 printf("Case #%d: ",t++); 76 if(OK)printf("Find\n"); 77 else printf("Impossible\n"); 78 } 79 return 0; 80 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************