Farmer John wants to evaluate the quality of the names of his N (1 <= N <= 1000) cows. Each name is a string with no more than 1000 characters, all of which are non-blank.
He has created a set of M (1 <= M <= 100) 'good' strings (no longer than 30 characters and fully non-blank). If the sequence letters of a cow's name contains the letters of a 'good' string in the correct order as a subsequence (i.e., not necessarily all next to each other), the cow's name gets 1 quality point.
All strings is case-insensitive, i.e., capital letters and lower case letters are considered equivalent. For example, the name "Bessie" contains the letters of "Be", "sI", "EE", and "Es" in the correct order, but not "is" or "eB". Help Farmer John determine the number of quality points in each of his cow's names.
Input
There are multiple tests. For each test, there are three parts.
- * Line 1: Two space-separated integers: N and M
- * Lines 2..N+1: Line i+1 contains a string that is the name of the ith cow
- * Lines N+2..N+M+1: Line N+i+1 contains the ith good string
Output
For each test, you should output following part.
* Lines 1..N+1: Line i+1 contains the number of quality points of the ith name
Sample Input5 3
Bessie
Jonathan
Montgomery
Alicia
Angola
se
nGo
Ont
Sample Output1
1
2
0
1
Original: Summer Training Qualification II
暴力过的............欢迎提供更好的算法-______________________-
- #include <stdio.h>
- #include <string.h>
- char cow[1001][1001],good[101][31];
- int point(char *a, char *b)
- {int lena,lenb,i,j,k;
- lena=strlen(a),lenb=strlen(b);
- if(lena<lenb) return 0;
- if(lena==lenb) if(strcmp(a,b)==0) return 1;else return 0;
- k=0;
- for(i=0;i<lenb;i++)
- {
- for(j=k;j<lena;j++)
- if(b[i]==a[j])
- {k=j+1;break;}
- if(j==lena) return 0;
- }
- return 1;
- }
- void lcase(char *a)
- {
- int len=strlen(a)-1;
- int i;
- for(i=0;i<=len;i++)
- if(a[i]>='A'&&a[i]<='Z') a[i]+=32;
- }
- int main()
- {int cn,gn,i,j,res;
- while(scanf("%d%d",&cn,&gn)!=EOF)
- {
- getchar();
- for(i=0;i<cn;i++)
- gets(cow[i]),lcase(cow[i]);
- for(i=0;i<gn;i++)
- gets(good[i]),lcase(good[i]);
- for(i=0;i<cn;i++)
- {res=0;
- for(j=0;j<gn;j++)
- res+=point(cow[i],good[j]);
- printf("%d/n",res);
- }
- }
- return 0;
- }
|