HDU2572终曲(枚举+STL)

21 篇文章 0 订阅
8 篇文章 0 订阅

题目链接hdu2572

解题思路:水题…但是WA了好几发,一直想在原串中剪切,然后得到符合的串。不过要考虑的case很多。数据规模小,完全可以暴力枚举所有字串,然后匹配。


AC代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	
	int T;
	string _union,str1,str2;
	
	cin >> T;
	while (T--) {
		cin >> _union >> str1 >> str2;
		
		int f1 = _union.find(str1);
		int f2 = _union.find(str2);
		
		if(f1 == string::npos || f2 == string::npos){
			cout << "No" << '\n';
			continue;
		}else	if(str1.find(str2) != string::npos) {
			cout << str1 << '\n';
			continue;
		}else if(str2.find(str1) != string::npos) {
			cout << str2 << '\n';
			continue;
		}
		
		string tmp,ans(_union);
		for (string::iterator it1 = _union.begin(); it1 != _union.end(); it1++) {
			for (string::iterator it2 = _union.begin(); it2 <= it1; it2++) {
				tmp = _union.substr(it2 - _union.begin(),it1 - it2 + 1);
//				cout << "tmp = " << tmp << endl;
				if(tmp.find(str1) != string::npos && tmp.find(str2) != string::npos) {
					if(ans.size() > tmp.size() || ans.size() == tmp.size() && ans > tmp) {
						ans = tmp;
					}
				}
			}
		}
		cout << ans << '\n';
	}
	return 0;
}

BUG代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int T, index_start, len, flag;
    char pub[1100], str1[200], str2[200], tmp[1100];
    scanf ("%d", &T);
    getchar();
    while (T--) {
        gets (pub);
        gets (str1);
        gets (str2);
        len = strlen(pub);
        index_start = 0;
        flag = 1;
        for (int i = strlen(str1); i <= len && flag; i++) {
            for (int j = 0; j <= len-i && flag; j++) {
                strncpy (tmp, pub + j, i);
                if(strstr(tmp, str1) && strstr(tmp, str2)) {
                    index_start = j;
                    len = i;
                    flag = 0;
                }
            }
        }

        if(flag) {
            puts("No");
        }else {
            pub[index_start + len] = 0;
            puts(pub+index_start);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胡同的诗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值