字符串旋转和追加
有人看懂了这一道题吗?
能稍微讲解下解题思路吗?
我不知道两种步骤怎么随意走 而且如果是不可能 那么什么时候算停止呢?
[此贴子已经被作者于2019-1-23 17:30编辑过]
#include <stdio.h> #include <string.h> #define LENTH 1000 int main(void) { int judge(char *, char *); char s[LENTH], t[LENTH]; printf("输入字符串s、t:\n"); scanf("%s%s", s, t); if (judge(s, t)) printf("Possible\n"); else printf("Impossible\n"); return 0; } int judge(char *s, char *t) { unsigned num_B_s, num_B_t; unsigned D_value_B, front_B_should, front_B; size_t len_s, len_t; size_t i, j; len_s = strlen(s); len_t = strlen(t); for (i = 0, num_B_s = 0; i < len_s; i++) if (s[i] == 'B') ++num_B_s; for (i = 0, num_B_t = 0; i < len_t; i++) if (t[i] == 'B') ++num_B_t; if (num_B_s > num_B_t) return 0; else if (num_B_s == num_B_t) { for (i = 0; i < len_s; i++) if (s[i] != t[i]) break; if (i == len_s) return 1; else return 0; } else { D_value_B = num_B_t - num_B_s; front_B_should = D_value_B / 2 + D_value_B % 2; for (i = 0, front_B = 0; ; i++) { if (t[i] != 'B') continue; if (++front_B == front_B_should) break; } if (D_value_B % 2) { for (j = 0; j < len_s; j++) if (s[j] != t[i + len_s - j]) break; if (j == len_s) return 1; else return 0; } else { for (++i, j = 0; j < len_s; j++) if (s[j] != t[i + j]) break; if (j == len_s) return 1; else return 0; } } }
[此贴子已经被作者于2019-1-24 17:14编辑过]