例题9-7(uva-11584)

#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int MAXSIZE = 1080;
char ch[MAXSIZE];
bool a[MAXSIZE][MAXSIZE];
int n,len,dp[MAXSIZE];

void Input() {
	cin >> n;
	cin.get();
}
void IsOk(int first,int last) {
	while (first > 0 && last <= len && ch[first] == ch[last]) {
		//first - last  是否为回文
		a[first][last] = 1;
		--first;
		++last;
	}
}
int main()
{
	Input();
	while (n--) {
		gets(ch+1);
		len = strlen(ch+1);

		for (int i = 1; i <= len; ++i)
		{
			IsOk(i, i);
			IsOk(i, i + 1);
		}

		dp[0] = 0;
		for (int i = 1; i <= len; ++i) {
			dp[i] = dp[i - 1] + 1;
			for (int j = 0; j < i; ++j) {
				if (a[j+1][i]) {
					dp[i] = min(dp[i], dp[j] + 1);
				}
			}
		}
		cout << dp[len] << endl;
		memset(a, 0, sizeof(a));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个实现该输出的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> class myclass { public: T operator()(T a, T b) { return a + b; } }; int main() { vector<int> v1 = {1, 2, 3, 4, 5}; vector<int> v2 = {6, 7, 8, 9, 10}; vector<int> v3(5); transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), myclass<int>()); for (auto& x : v3) { cout << x << " "; } cout << endl; vector<double> v4 = {1.1, 2.2, 3.3, 4.4, 5.5}; vector<double> v5 = {6.6, 7.7, 8.8, 9.9, 10.0}; vector<double> v6(5); transform(v4.begin(), v4.end(), v5.begin(), v6.begin(), myclass<double>()); for (auto& x : v6) { cout << x << " "; } cout << endl; return 0; } ``` 在这个示例代码中,我们定义了一个类模板myclass,它有一个模板参数T,并重载了()运算符。在()运算符中,我们将两个参数相加,并返回结果。 在主函数中,我们定义了两个整型向量v1和v2,以及一个整型向量v3。我们使用transform函数将v1和v2中的元素依次相加,并将结果存储在v3中。然后,我们遍历v3中的元素,并将其输出到控制台上。 接着,我们定义了两个双精度浮点型向量v4和v5,以及一个双精度浮点型向量v6。我们再次使用transform函数将v4和v5中的元素依次相加,并将结果存储在v6中。最后,我们遍历v6中的元素,并将其输出到控制台上。 最终,程序输出的结果如下: ``` 7 9 11 13 15 7.7 9.9 12.1 14.3 15.5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值