20180516模拟赛T2——string

题解

对于一个字符串A,我们只能把其首字符取出,故如果我们想让A串与B串相等,能重复利用的部分只能是A串结尾与B串开头相等的部分。对于取出的字符,我们可以把’o’放在一个容器中,把’x’放在另一个容器中,要用时取出对应的即可。

由于不重复利用的字符最少是取出一次、放回一次,而我们构造出的方法仅需花费这样的代价,故此为最优解。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char a[105], b[105];

int main()
{
    freopen("string.in", "r", stdin);
    freopen("string.out", "w", stdout);
    int n;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%s%s", a+1, b+1);
        int len = strlen(a+1);
        int ans = len;
        while(ans)
        {
            bool f = true;
            for(int i = 1; i <= ans; ++i)
                if(b[i] != a[len-ans+i])
                {
                    f = false;
                    break;
                }
            if(f) break;
            ans--;
        }
        printf("%d\n", (len-ans)<<1);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/pfypfy/p/9048217.html