티스토리 뷰

digitalization

swea 아나그램

김신 2019. 8. 26. 12:52

요소들이 등장하는 개수만 카운팅하여 비교함.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>>
char S1[100001];
char S2[100001];
int main(int argc, const char * argv[]) {
    int T =0;
    scanf("%d"&T);
    char c;
    for(int test = 1; test<=T; test++){
        
        int cnt=0;
        while(1){
            scanf("%c",&c);
            if(c>='a'&&c<='z'){
                S1[cnt]= c;
                cnt++;
            }
            else{
                if(cnt!=0)
                break;
            }
           
        }
        int len1 = cnt;
        cnt=0;
        
        while(1){
            scanf("%c",&c);
            if(c>='a'&&c<='z'){
                S2[cnt]= c;
                cnt++;
            }
            else{
                if(cnt!=0)
                break;
            }
            
        }
        
        int len2 = cnt;
        int table1[26]={0,};
        int table2[26]={0,};
        
        int r=0;
        for(register int i =0; i<len1;i++){
            table1[ S1[i] - 'a' ]++;
            table2[ S2[i] - 'a' ]++;
        }
        for(register int i=0;i<26;i++){
            if(table2[i] != table1[i]) {
                r=1;
                break;
            }
        }
        if(r==1) r=0;
        else r=1;
        for(register int i=0; i<len2-len1;i++){
            table2[ S2[i] -'a' ]--;
            table2[ S2[i+len1] -'a']++;
            int det =1;
            for(register int j=0;j<26;j++){
                if(table2[j] != table1[j]) {
                    det =0;
                    break;
                }
            }
            r+=det;
        }
        printf("#%d %d\n", test, r);
        
 
    }
    return 0;
}
 
cs

댓글
공지사항
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
링크
글 보관함