注意:当输入只有一个,且这一个还是个坏键,比如第一行输入3,第二行什么都不输入,回车。还有注意,坏的键全在第一行比第二行长的部分,比如坏键是123,第一行输入456123,第二行输入456
#include <stdio.h>
#include <string.h>
#define GOOD 2
#define BAD 1
#define MAX_STRING 81
char input[MAX_STRING];
char test[MAX_STRING];
int len;
int read_input ();
int deal_result ();
int print_result ();
/*-------------------- 将输出读进来,并做相关标记 ------------------------*/
int read_input ()
{
int i;
char c;
for (i = 0; (c = getchar()) != '\n'; ++i)
{
if (c >= 'a' && c <= 'z')
c -= 32;
input[i] = c;
}
len = i;
for (i = 0; (c = getchar()) != '\n'; ++i)
{
if (c >= 'a' && c <= 'z')
c -= 32;
while (c != input[i] && i < len)
{
test[i] = BAD;
++i;
}
test[i] = GOOD;
}
for (; i < len; ++i)
test[i] = BAD;
return 0;
}
/*----------------------- 处理结果,坏键只输出一次 -----------------------*/
int deal_result ()
{
int i, j;
char c;
for (i = 0; i < len; ++i)
{
if (test[i] == BAD)
{
c = input[i];
for (j = len-1; j > i; --j) /* 并把后面相同的键也标记了, */
if (input[j] == c) /* 是后面出现的坏键 */
test[j] = GOOD; /* 标记该坏键是好的,因为只输出该坏键一次(i的位置是那一次)即可 */
}
}
return 0;
}
/*---------------------------- 输出结果 -------------------------------*/
int print_result ()
{
int i;
for (i = 0; i < len; ++i)
if (test[i] == BAD)
putchar(input[i]);
return 0;
}
int main ()
{
read_input ();
deal_result ();
print_result ();
return 0;
}