PAT 1093 字符串A+B

1093 字符串A+B

给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。

输入格式:
输入在两行中分别给出 A 和 B,均为长度不超过 10^​6的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。

输出格式:
在一行中输出题面要求的 A 和 B 的和。

输入样例:

This is a sample test
to show you_How it works

输出样例:

This ampletowyu_Hrk

代码如下(c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 1000001
#define N 2000002
char a[N],b[M];
char c[N];//栈
int main()
{
    gets(a);
    gets(b);
    strcat(a,b);
    int len = strlen(a);
    int i;
    c[0] = a[0];
    int k=1;
    for(i=1;i<len;i++){
        int j;
        int flag = 0;
        for(j=0;j<k;j++){
           if(c[j]==a[i]){
             flag = 1;
             break;
           }
        }
        if(flag==1) continue;
        else{
            c[k++] = a[i];
        }
    }
    c[k]='\0';
    puts(c);
    return 0;
}

总结:插入排序思想

猜你喜欢

转载自blog.csdn.net/YYLong0/article/details/86407435