intさわだんのBlack History

刹那的レジェンドになりたい。

2

  • 関数 check_strings() は大文字と小文字の違いを無視して二つの文字列が一致するかどうかを判定する。
  • int aa,bb; は単語の重複を記録しておくための配列(すべて0で初期化されている。)これをやらないと意地の悪い入力で間違った答えがでる。
  • なんかもっとスマートなやりかたある気がする。というかあってほしい。
#include <stdio.h>
#include <string.h>


int check_strings(char a[],char b[]){
    if(strlen(a) != strlen(b)) return 0;
    int i;
    char ca,cb;
    for(i = 0;i < strlen(a);i++){
        if(a[i] >= 65 && a[i] <= 90)ca = a[i] + 32;
        else ca = a[i];
        if(b[i] >= 65 && b[i] <= 90)cb = b[i] + 32;
        else cb = b[i];
        
        if(ca != cb) return 0;
    }
    return 1;
}

int main(void) {
    
    int n,i,j,ans = 0,asum = 0,bsum = 0,an,bn;
    char a[11][22],b[11][22];
    int aa[11] = {0},bb[11] = {0};
    
    printf("Input a number of words as n : ");
    scanf("%d",&n);
    an = n;
    bn = n;   
    
    printf("Input %d words for list 1 : ",n);
    for(i = 0;i < n;i++){
        scanf("%s",a[i]);
    }
    
    printf("Input %d words for list 2 : ",n);
    for(i = 0;i < n;i++){
        scanf("%s",b[i]);
    }
    
    for(i = 0;i < n;i++){
        if(aa[i])continue;
        for(j = i + 1;j < n;j++){
            if(check_strings(a[i],a[j])){
                aa[j] = 1;
                an--;
            }
        }
    }

    for(i = 0;i < n;i++){
        if(bb[i])continue;
        for(j = i + 1;j < n;j++){
            if(check_strings(b[i],b[j])){
                bb[j] = 1;
                bn--;
            }
        }
    }
        
    if(an != bn){
        puts("NO, they are different word lists!");
        return 0;
    }
    

    for(i = 0;i < n;i++){
        if(aa[i])continue;
        for(j = 0;j < n;j++){
            if(bb[j])continue;
            if (check_strings(a[i],b[j])){
                ans++;
                break;
            }
        }
    }
    
    if(ans == an) puts("YES, each list has the same words!");
    else puts("NO, they are different word lists!");
	
	return 0;
}