2018四川省大学程序设计竞赛(ACM)B: Beyond the Boundry ----------C语言——菜鸟级

欢迎前往个人搭建博客 Five-great的博客

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Five-菜鸟级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值