zoukankan      html  css  js  c++  java
  • 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
    ", (len-ans)<<1);
    	}
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    使用合理jQuery选择器查找DOM元素
    DOM对象和jQuery对象
    jQuery实现返回顶部
    行内元素,块级元素
    图片自适应缩放
    幽灵按钮
    background-attachment:fixed
    RegExp
    正则
    Date
  • 原文地址:https://www.cnblogs.com/pfypfy/p/9048217.html
Copyright © 2011-2022 走看看