Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1040 Longest Symmetric String] may be #3

Open
absop opened this issue Sep 7, 2019 · 2 comments
Open

[1040 Longest Symmetric String] may be #3

absop opened this issue Sep 7, 2019 · 2 comments

Comments

@absop
Copy link

absop commented Sep 7, 2019

#include <stdio.h>

int main()
{
    int longest = 1;
    char line[1024] = {0};
    gets(line + 1); /* line[0] as sentinel */

    char *lo, *hi;
    for (char *p = line + 1; *p; ++p) {
        for (lo = p; *lo == *p; --lo);
        for (hi = p; *hi == *p; ++hi);
        for (; *lo == *hi && *lo && *hi; --lo, ++hi);
        if (hi - lo - 1 > longest) longest = hi - lo - 1;
    }
    printf("%d\n", longest);

    return 0;
}
@tiny656 tiny656 changed the title 1040 may be [1040 Longest Symmetric String] may be Sep 13, 2019
@tiny656
Copy link
Owner

tiny656 commented Sep 13, 2019

#include <stdio.h>

int main()
{
    int longest = 1;
    char line[1024] = {0};
    gets(line + 1); /* line[0] as sentinel */

    char *lo, *hi;
    for (char *p = line + 1; *p; ++p) {
        for (lo = p; *lo == *p; --lo);
        for (hi = p; *hi == *p; ++hi);
        for (; *lo == *hi && *lo && *hi; --lo, ++hi);
        if (hi - lo - 1 > longest) longest = hi - lo - 1;
    }
    printf("%d\n", longest);

    return 0;
}

代码很简洁。😀

@absop
Copy link
Author

absop commented Sep 13, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants