1029. 旧键盘

题目截图:

思路:

  字符串匹配。先找出所有未输出字符,然后按序输出。设置一个数组标记字符是否已输出,若已输出则不再输出。注意要将小写字符转成大写字符。

代码:

 1 /*
 2     1029. 旧键盘
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 
11 #define maxn 82
12 // str1为应输入的文字,str2为实际输入的文字,str3为未输出的字符 
13 char str1[maxn], str2[maxn], str3[maxn]; 
14 int asc[256] = {0};            // 标记字符是否已输出 
15 
16 int main() {
17     int i=0, j=0, cnt=0;
18     scanf("%s %s", str1, str2);
19     // 找未输出字符 
20     while(i<strlen(str2) && j<strlen(str1)) {
21         if(str2[i] != str1[j]) {        // 字符未输出 
22             char temp = str1[j];
23             if(temp>='a'&&temp<='z') {    // 小写字母转大写 
24                 temp -= 32;
25             }
26             str3[cnt++] = temp;            // 将未输出字符即坏键存储 
27             j++;
28         } else {
29             i++;j++;
30         }
31     }
32     if(j<strlen(str1)) {                // str1 后面的字符均为坏键 
33         for(;j<strlen(str1); ++j) {
34             char temp = str1[j];
35             if(temp>='a'&&temp<='z') {
36                 temp -= 32;
37             }
38             str3[cnt++] = temp;
39         }
40     }
41     for(i=0; i<cnt; ++i) {                // 按发现顺序,输出坏键 
42         if(!asc[str3[i]-'']) {
43             printf("%c", str3[i]);
44             asc[str3[i]-''] = 1;
45         }
46     }
47     
48 
49     return 0;
50 }
原文地址:https://www.cnblogs.com/coderJiebao/p/PAT1029.html