版权声明:本文为博主原创文章,未经博主允许不得转载。
Titles:
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!
Constraints:
1 <= T <= 10000
Example
standard input:
3
NaseMitsuki
a
ka
standard output:
1
Nase Mitsuki
4
Kanbara Akihito
Kuriyama Mirai
Nase Hiroomi
Nase Mitsuki
2
Kanbara Akihito
Kuriyama Mirai
解题思路:
最开始的时候,自己想写如下这样一个结构体来将是否满足和相应的字符串进行绑定。
struct node
{
char name[20];
int right;
}stu[5];
后来发现自己不会对结构体数组里面的字符串进行赋值,就放弃了这个方法。
进而发现这四个同学名字的字符串可以在复制的时候,就排好字典序,那么在程序执行的过程中,就只需要判断其是否含有相应的上升子序列就可以了。因此改用了bool类型的变量来进行判断。之后就纯暴力(当然还是要注意下,我们赋值输入的字符串是含有空格的,跳过即可)。
代码实现如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
char name[5][20] = {"Kanbara Akihito","Kuriyama Mirai","Nase Hiroomi","Nase Mitsuki"};
char str[20];
int t,num,ans,j,i;
scanf("%d",&t);
while(t--)
{
bool f1 = 1,f2 = 1,f3 = 1,f4 = 1;
int pos = 0,num = 0,ans = 0;
scanf("%s",str);
int len = strlen(str);
for(i = 0;i < len; i++)
{
for(j = pos; j < 15; j++)
{
if(name[0][j] == ' ')
continue;
if(str[i] == name[0][j])
{
num++;
pos = j + 1;
break;
}
}
}
if(num >= len)
ans++;
else
f1 = 0;
num = 0;
pos = 0;
for(i = 0;i < len; i++)
{
for(j = pos; j < 15; j++)
{
if(name[1][j] == ' ')
continue;
if(str[i] == name[1][j])
{
num++;
pos = j + 1;
break;
}
}
}
if(num >= len)
ans++;
else
f2 = 0;
num = 0;
pos = 0;
for(i = 0;i < len; i++)
{
for(j = pos; j < 15; j++)
{
if(name[2][j] == ' ')
continue;
if(str[i] == name[2][j])
{
num++;
pos = j + 1;
break;
}
}
}
if(num >= len)
ans++;
else
f3 = 0;
num = 0;
pos = 0;
for(i = 0;i < len; i++)
{
for(j = pos; j < 15; j++)
{
if(name[3][j] == ' ')
continue;
if(str[i] == name[3][j])
{
num++;
pos = j + 1;
break;
}
}
}
if(num >= len)
ans++;
else
f4 = 0;
printf("%d\n",ans);
if(f1)
puts(name[0]);
if(f2)
puts(name[1]);
if(f3)
puts(name[2]);
if(f4)
puts(name[3]);
}
return 0;
}