Uva11584 Partitioning by Palindromes

Partitioning by Palindromes

 We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concatenation yields the initial sequence. For example, (‘race’, ‘car’) is a partition of ‘racecar’ into two groups. Given a sequence of characters, we can always create a partition of these characters such that each group in the partition is a palindrome! Given this observation it is natural to ask: what is the minimum number of groups needed for a given string such that every group is a palindrome? For example: • ‘racecar’ is already a palindrome, therefore it can be partitioned into one group. • ‘fastcar’ does not contain any non-trivial palindromes, so it must be partitioned as (‘f’, ‘a’, ‘s’, ‘t’, ‘c’, ‘a’, ‘r’). • ‘aaadbccb’ can be partitioned as (‘aaa’, ‘d’, ‘bccb’). Input Input begins with the number n of test cases. Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within. Output For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes. Sample Input 3 racecar fastcar aaadbccb Sample Output 1 7 3

 

https://odzkskevi.qnssl.com/5cc02c8be522c16812da27d55b5790ce?v=1508140214

 

【题解】

预处理ok[i][j]表示i到j是否组成回文串,枚举中间点即可

注意偶数回文串的情况

dp[i]表示前i个数的最小划分,转移即可

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 #define max(a, b) ((a) > (b) ? (a) : (b))
 8 #define min(a, b) ((a) < (b) ? (a) : (b))
 9 inline void swap(int &a, int &b)
10 {
11     int tmp = a;a = b;b = tmp;
12 }
13 inline void read(int &x)
14 {
15     x = 0;char ch = getchar(), c = ch;
16     while(ch < '0' || ch > '9')c = ch, ch = getchar();
17     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
18     if(c == '-')x = -x;
19 }
20 
21 const int INF = 0x3f3f3f3f;
22 const int MAXN = 1000 + 10;
23 
24 int t,n,dp[MAXN], ok[MAXN][MAXN];
25 char s[MAXN];
26 
27 int main()
28 {
29     read(t);
30     for(;t;--t)
31     {
32         memset(ok, 0, sizeof(ok));
33         scanf("%s", s + 1);
34         n = strlen(s + 1);
35         for(register int i = 1;i <= n;++ i)
36         {
37             int j = i;
38             int tmp = i;
39             while(s[tmp] == s[j] && tmp >= 1 && j <= n)
40             {
41                 ok[tmp][j] = 1;
42                 -- tmp, ++ j;
43             }
44             tmp = i;
45             j = tmp + 1;
46             while(s[tmp] == s[j] && tmp >= 1 && j <= n)
47             {
48                 ok[tmp][j] = 1;
49                 -- tmp, ++ j;
50             }
51         } 
52         for(register int i = 1;i <= n;++ i)
53         {
54             dp[i] = INF;
55             for(register int j = 1;j <= i;++ j)
56                 if(ok[j][i]) dp[i] = min(dp[i], dp[j - 1] + 1);
57         }
58         printf("%d\n", dp[n]);
59     }
60     return 0;
61 }
Uva11584

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7691127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值