1040 count the number of words

Source: Internet
Author: User

1040 Count the number of words

2001 NOIP National League Improvement Group

time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingTitle Description Description

Give a letter string with a length of not more than 200, consisting of a lowercase English letter (contract; The string is entered in 20 letters per line and guaranteed to be 20 each). This letter is required to be divided into K-parts (1<k<=40), and the number of words contained in each copy is the largest (the words contained in each copy can be partially overlapped). After a word is selected, its first letter cannot be used again. For example, the string this can contain this and is, after selecting this cannot contain th) (Administrator Note: Here can not be used to refer to the position, not the letter itself. For example, thisis can be counted as including 2 is).
The word is given in a dictionary of no more than 6 words.
The maximum number of outputs required.

Enter a description Input Description

First behavior a positive integer (0<n<=5) indicates that there are n sets of test data
The first row of each group has two positive integers (p,k)
P indicates the number of lines of string;
K is divided into k parts.
The next P-line, each line has 20 characters.
Then there is a positive integer s, which indicates the number of words in the dictionary. (1<=s<=6)
The next S-line, each line has one word.

Output description Output Description

Each row is an integer that corresponds to the corresponding result for each set of test data.

Sample input Sample Input

1
1 3
Thisisabookyouareaoh
4
Is
A
Ok
Sab

Sample output Sample Output

7

Data range and Tips Data Size & Hint

This/isabookyoua/reaoh

Category labels Tags Click here to expandPartition DP Dynamic Planning Continental region Noip National League increase Group 2001

AC Code:

/*Solution : Divide the string ss[0..len-1] into the optimal value of the K section, consider dividing the first I characters into the optimal value F (i,j) =max{f (i-x,j-1) + the number of words in X characters} (I>=j and x>=) of J parts. 1,I-X>=J-1) is 1<=x<i-j for the interval [ii.. JJ] contains the number of words, one by one to S[kk] (II<=KK<=JJ) The beginning of the word can be,*/#include<cstdio>#include<iostream>#include<cstring>using namespacestd;#defineN 211intn,p,k,s,f[n][ A],len;Charss[n],tt[n],w[ A][n]; voidFirst () {memset (F,0,sizeof(f));//develop a good habit of clearing 0Memset (W,0,sizeof(w)); memset (TT,0,sizeof(TT)); memset (SS,0,sizeof(ss)); scanf ("%d%d",&p,&k); Len=0;  for(intI=0; i<p;i++) {scanf ("%s", TT);  for(intj=0; tt[j];j++) {Ss[len]=tt[j];len++;//string Pressure stack}} Ss[len]=' /'; scanf ("%d", &s);//number of words     for(intI=0; i<s;i++) scanf ("%s", W[i]);//Word}intdpintXinty) {    intans=0, cur;  for(inti=x;i<=y;i++){         for(intj=0; j<s;j++){             for(cur=0; w[j][cur];cur++)if(i+cur>y| | W[j][cur]!=ss[i+cur]) Break; if(w[j][cur]==' /') {ans++; Break;//sentence The last of these words            }            }    }        returnans;}intMain () {scanf ("%d",&N);  while(n--) {first ();  for(intI=1; i<=len;i++) f[i][1]=DP (0, I-1);//Initialize F         for(intj=2; j<=k;j++) {//Note I j Order             for(inti=j;i<=len;i++){                 for(intx=1; x<i-j;x++) {//Enumeration Sub-method                    intTMP=DP (i-x,i-1); F[I][J]=max (f[i][j],f[i-x][j-1]+tmp); }}} printf ("%d\n", F[len][k]); }    return 0;}

1040 Count Words

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.