Almost Palindrome UVA

来源:互联网 发布:java转义html特殊字符 编辑:程序博客网 时间:2024/06/04 19:27

Almost Palindrome UVA - 12656

题目链接:https://cn.vjudge.net/problem/UVA-12656

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cctype>using namespace std;const int maxn = 1e3 + 10;int main(void){    int k, kase = 0;    while (cin >> k) {        string s;        char al[maxn] = {0};        int pl[maxn] = {0};        getchar();        getline(cin, s);        int tp = 1;        for (int i = 0; i < (int)s.length(); i++) {            if (isalpha(s[i])) {                al[tp] = tolower(s[i]);                pl[tp++] = i + 1;            }        }        int mlen = 0, mp;        for (int i = 1; i < tp; i++) {            int p = i, q = i;            int tk = 0;            while (p >= 1 && q < tp) {                if (al[p] == al[q])                    p--, q++;                else if (tk < k)                    p--, q++, tk++;                else                    break;            }            p++, q--;            if (mlen < pl[q] - pl[p] + 1) {                mlen = pl[q] - pl[p] + 1;                mp = pl[p];            }            p = i, q = i + 1;            tk = 0;            while (p >= 1 && q < tp) {                if (al[p] == al[q])                    p--, q++;                else if (tk < k)                    p--, q++, tk++;                else                    break;            }            p++, q--;            if (p < q && mlen < pl[q] - pl[p] + 1) {                mlen = pl[q] - pl[p] + 1;                mp = pl[p];            }        }        printf("Case %d: %d %d\n", ++kase, mlen, mp);    }    return 0;}