Partitioning by Palindromes UVA - 11584
2017-09-12 22:14
351 查看
读入相应的字符串,然后以读入的每一个字符串的字符为起点,以构成偶数长度和奇数长度的方式来找回文串,找到之后对于该位置字符的计数进行相应的更新即可,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
int n;
int dp[1010];
int main(){
cin >> n;
while (n--){
string s;
cin >> s;
fill(dp, dp + 1010, 1 << 20);
s = "0" + s;
dp[0] = 0;
for (int i = 1; i < s.size(); i++){
int left, right;
for (left = i, right = i; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
for (left = i, right = i + 1; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
}
cout << dp[s.size() - 1] << endl;
}
return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
int n;
int dp[1010];
int main(){
cin >> n;
while (n--){
string s;
cin >> s;
fill(dp, dp + 1010, 1 << 20);
s = "0" + s;
dp[0] = 0;
for (int i = 1; i < s.size(); i++){
int left, right;
for (left = i, right = i; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
for (left = i, right = i + 1; left > 0 && right < s.size() && s[left] == s[right]; left--, right++)
dp[right] = min(dp[right], dp[left - 1] + 1);
}
cout << dp[s.size() - 1] << endl;
}
return 0;
}
相关文章推荐
- UVA 11584 Partitioning by Palindromes 回文串dp
- uva 11584 Partitioning by Palindromes dp
- uva 11584 Partitioning by Palindromes 回文+dp
- Uva-11584-Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes 回文串dp
- UVA11584---Partitioning by Palindromes(dp)
- UVA 11584 Partitioning by Palindromes 区间dp
- UVA 11584 Partitioning by Palindromes
- UVA 题目11584 - Partitioning by Palindromes(DP)
- uva 11584 - Partitioning by Palindromes(简单dp)
- Uva 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes DP
- uva 11584——Partitioning by Palindromes
- UVa11584 Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes(简单DP)
- 【动态规划】[UVa11584]Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- uva_11584_Partitioning by Palindromes( DP )
- Uva 11584 Partitioning by Palindromes (简单DP)
- UVA 11584 Partitioning by Palindromes