Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用两个指针一起走就行了。附代码,字符串功底不扎实的童鞋可进来学习

Posted by Eov_Second at 2016-12-01 17:31:30 on Problem 3302
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator