HDU 3718 (km algorithm)

Source: Internet
Author: User

A relatively basic question. Template

 

Similarity

Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1076 accepted submission (s): 411

Problem descriptionwhen we were children, we were always asked to do the classification homework. for example, we were given words {tiger, panda, potato, dog, tomato, pea, apple, pear, orange, Mango} and we were required to classify these words into three groups. as you know, the correct classification was {tiger, panda, dog}, {potato, tomato, pea} and {apple, pear, orange, Mango }. we can represent this classification with a mapping sequence {A, B, A, B, B, C}, and it means tiger, panda, dog belong to Group A, potato, tomato, pea are in the group B, and apple, pear, orange, Mango are in the group C.
But the label of group doesn' t make sense and the label is just used to indicate different groups. so the representations {P, P, O, P, O, O, Q} and {e, E, F, E, F, F, W, w, W, w} are equivalent to the original Mapping Sequence. however, the representations {a, B, B, C} and
{D, D, G} are not equivalent.

The pupils in class submit their mapping sequences and the teacher shoshould read and grade the homework. the teacher grades the homework by calculating the maximum similarity between pupils 'ing ing sequences and the answer sequence. the definition of similarity is as follow.

Similarity (s, t) = sum (SI = Ti)/L
L = length (S) = length (t), I = 1, 2,... l,
Where sum (SI = Ti) indicates the total number of equal labels in corresponding positions. the maximum similarity means the maximum similarities between S and all equivalent sequences of T, where S is the answer and fixed. now given all sequences submitted by pupils and the answer sequence, you shoshould calculate the sequences 'maximum similarity.

 

Inputthe input contains multiple test cases. the first line is the total number of cases T (t <15 ). the following are t blocks. each block indicates a case. A case begins with three numbers N (0 <n <10000), K (0 <k <27), m (0 <m <30 ), which are the total number of objects, groups, and students in the class. the next line consists of N labels and each label is in the range [... z]. you can assume that the number of different labels in the sequence is exactly K. this sequence represents the answer. the following are m lines, each line contains N labels and each label also is in the range [... z]. these M lines represent the M pupils 'answer sequences. you can assume that the number of different labels in each sequence doesn' t exceed K.

 

Outputfor each test case, output M lines, each line is a floating number (round to 4 digits after the decimal point). You shocould output the M answers in the order of the sequences appearance.

 

Sample input2
10 3 3
A a B a B c
F e f e d
X y z
S t r s
3 2 2
A B
C D C
F E

 

Sample output1.0000
0.7000
0.5000
1.0000
0.6667

 

Authorlin, Yue

 

Source2010 Asia Chengdu Regional Contest

 

Recommendzhouzeyong
# Include <stdio. h> # Include < String . H> # Include <Iostream> Using   Namespace  STD;  # Define INF 0x3fffffff # Define N 33 Int Link [ 30 ], Link1 [ 30 ];  Char Tlink [ 30  ];  Int G [ 30 ] [ 30  ];  Int  Len, K, Q;  Char STR [ 10010 ], Str1 [ 10010  ];  Int  N, m; Int  Markx [N], Marky [N];  Int  Mark [N];  Int  Wx [N], WY [N];  Int  Save [N];  Int  Pre [N];  Int DFS ( Int  S) {markx [s] = 1  ;  For (Int I = 1 ; I <= m; I ++ ){  If (Wx [s] + WY [I]-G [s] [I] <save [I]) Save [I] = wx [s] + WY [I]- G [s] [I];  If (MARK [I] = 1 | Wx [s] + WY [I]! = G [s] [I]) Continue  ; Mark [I] = 1  ; Marky [I] = 1  ; If (Pre [I] =- 1 | DFS (pre [I]) {pre [I] = S;  Return   1  ;}}  Return   0  ;}  Int  Km () {memset (PRE, - 1 , Sizeof  (Pre); memset (wx, 0 , Sizeof  (Wx); memset (WY,  0 , Sizeof  (WY ));  For ( Int I = 1 ; I <= N; I ++ )  For ( Int J = 1 ; J <= m; j ++ ) WX [I] =Max (wx [I], G [I] [J]);  For ( Int I = 1 ; I <= N; I ++ ){  While ( 1  ) {Memset (markx,  0 , Sizeof  (Markx); memset (Marky,  0 , Sizeof  (Marky); memset (mark, 0 , Sizeof  (Mark ));  For ( Int J = 1 ; J <= m; j ++ ) Save [J] = INF;  If (DFS (I) = 1 ) Break ; //  Because N is less, an endless loop may occur.              Int Mi =INF;  For ( Int J = 1 ; J <= m; j ++ )  If (Marky [J] = 0 & Save [J] <mi) MI = Save [J];  For ( Int J = 1 ; J <= N; j ++ )  If (Markx [J] =1 ) WX [J]-= Mi;  For ( Int J = 1 ; J <= m; j ++ )  If (Marky [J] = 1 ) WY [J] + = Mi ;}}  Int Sum = 0  ;  For ( Int I =1 ; I <= m; I ++ )  If (Pre [I]! =- 1  ) Sum + = G [pre [I] [I];  Return  SUM ;}  Int  Main (){  Int  T; scanf (  "  % D  " ,&T );  While (T -- ) {Memset (link,  0 , Sizeof  (Link); scanf (  "  % D  " , & Len, & K ,& Q );  For ( Int I = 0 ; I <Len; I ++ ) CIN >STR [I]; m = 0  ;  For ( Int I = 0 ; I <Len; I ++ ){  If (Link [STR [I]- '  A  ' ] = 0  ) {Link [STR [I] - ' A  ' ] = ++ M; tlink [m] = STR [I] ;}}  For ( Int I = 0 ; I <q; I ++ ) {Memset (G,  0 , Sizeof  (G); memset (link1,  0 , Sizeof (Link1 ));  For ( Int J = 0 ; J <Len; j ++ ) CIN > Str1 [J]; n = 0  ;  For ( Int I1 = 1 ; I1 <= m; I1 ++ )  For (Int J = 0 ; J <Len; j ++ ){  If (Link1 [str1 [J]- '  A  ' ] = 0 ) Link1 [str1 [J]- '  A  ' ] = ++ N;  If (STR [J] =Tlink [I1]) g [link1 [str1 [J] - '  A  ' ] [I1] ++ ;} Printf (  "  %. 4lf \ n  " ,( Double ) Km ()/( Double  ) Len );}}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.