Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用两个指针一起走就行了。附代码,字符串功底不扎实的童鞋可进来学习src和dst两个指针分别指向s1和s2。如果src指向的字符不等于dst的,那就src一直往前走直到相等;否则dst往前走一步。最后如果src先走到结尾,那么说明没找到,否则找到。 int main() { int t,bf; char s1[110], s2[110],cs,cd; s2[0] = 0; char *src, *dst; scanf("%d", &t); while (t--) { // 注意s2[0]保留,始终为'\0',这样方便反向遍历 scanf("%s %s", s1, s2+1); src = s1; dst = s2+1; bf = 1; // bf标记是否找到 // 每次拿s2的一个字符和s1当前位置的字符比较,如果不相等,s1一直向前移动直到相等或者结尾 while (cd = *dst++) { while (cs = *src++) { if (cs == cd) break; } if (0 == cs) { bf = 0; while (*dst)dst++; break; } } //第一遍正向没找到,s2反向再来一次 if (0 == bf) { bf = 1; src = s1; dst--; while (cd = *dst--) { while (cs = *src++) { if (cs == cd) break; } if (0 == cs) { bf = 0; break; } } } if (bf)printf("YES\n"); else printf("NO\n"); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator