1. 程式人生 > >2018四川省大學程式設計競賽(ACM)B: Beyond the Boundry ----------C語言——菜鳥級

2018四川省大學程式設計競賽(ACM)B: Beyond the Boundry ----------C語言——菜鳥級

B: Beyond the Boundry
Time Limit: 1000 MS Memory Limit: 1048576 KB
Total Submit: 98 Accepted: 51 Page View: 407
Submit Status Clarify

Description
There are four students, Kanbara Akihito, Kuriyama Mirai, Nase Hiroomi and Nase Mitsuki, studying in the senior middle school. They all took part in an exam the other day. Now it`s your turn to give papers back to them. However, the names were written so vaguely that you barely recognize any letters of their names.

Given a character string representing the vague name written on the paper, of which the blank has been omitted, you are supposed to output the possible original names of it (which means that after obliterating the blank and some of the characters of the person’s name, it turns into the vague name which is given). If multiple names match the vague name, output all of them in the lexicographical order.

Attention: uppercase letters and lowercase letters are distinctive!

Input
The first line contains a integer T
T
representing the number of test cases.
In each test case, there is a non-empty string s
s
in one line.
It is guaranteed that s contains only English letters and is a subsequence of at least one student’s name.
1≤T≤10000
1≤T≤10000

Output
For each test case, output an integer n in the first line representing the number of students satisfying that s is a subsequence of their names.
There are n lines following (in lexicographical order). Each of them contains a name which might be the original name of the vague name.

Sample Input

Raw

3
NaseMitsuki
a
Ka

Sample Output

Raw

1
Nase Mitsuki
4
Kanbara Akihito
Kuriyama Mirai
Nase Hiroomi
Nase Mitsuki
2
Kanbara Akihito
Kuriyama Mirai

思路: 分別判斷4個母串 中 是否存在 遞增子序列 與 匹配串相同

AC程式碼

#include <stdio.h>
#include <string.h>
int main()
{
    int T,i,j,k,now,ans,len;char str[4][20]={"Kanbara Akihito","Kuriyama Mirai","Nase Hiroomi","Nase Mitsuki"};
    int s[4][260][5];char ps[20];int res[5];
    memset(s,0,sizeof(s));
    for(i=0;i<4;i++)
     {  len=strlen(str[i]);
      for(j=0;j<len;j++)
       { k=0;
        while(s[i][str[i][j]][k])k++;//判斷是否前面出現過該字元 
          s[i][str[i][j]][k]=j+1;//位置從1開始 所以 j+1; 
		//在第i個字串中第 j 個字元在該字串的位置 
		 //0 1 2 3 4 5 6   j
		// K a n b a r a 
		// s[i][str[i][j]][k]=j+1;
		// s[0][str[0][6]][3]=j+1;
		// s[0][97][3]=7;
		// {s[0][97][2]=5;
		// s[0][97][3]=2;}
       }
	 }
    scanf("%d\n",&T);
    while(T--)
    {  memset(res,0,sizeof(res));ans=0;
        gets(ps);
        len=strlen(ps);
        for(i=0;i<4;i++)
        {   now=0;
            for(j=0;j<len;j++)
            {  k=0;
                while(now>=s[i][ps[j]][k]&&k<4)k++;//判斷是否有匹配但前字元且
                if(k==4)break;  //未匹配 成功     // 在母串中位置比上一個匹配字元位置大 
                now=s[i][ps[j]][k];//更新當前匹配 
            }
            if(j==len)res[i]=i+1,ans++;//符合條件 存入結果陣列 
        }
        printf("%d\n",ans);
        for(i=0;i<5;i++)
        if(res[i])printf("%s\n",str[res[i]-1]);
    }
    return 0;
}