Uva--10453(动规)

2014-08-05 00:15:23

Make Palindrome

Input: standard input

Output: standard output

Time Limit: 8 seconds

 

By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.

Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length 'n', no more than (n-1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are allowed to insert characters at any position of the string.

Input

Each input line consists only of lower case letters. The size of input string will be at most 1000. Input is terminated by EOF.

Output

For each input print the minimum number of characters and such a palindrome seperated by one space in a line. There may be many such palindromes. Any one will be accepted. 

Sample Input

abcd aaaa abc aab abababaabababa pqrsabcdpqrs

Sample Output

3 abcdcba 0 aaaa 2 abcba 1 baab 0 abababaabababa 9 pqrsabcdpqrqpdcbasrqp

Author : Md. Kamruzzaman

The Real Programmers' Contest-2

 

思路:经典回文DP,唯一要注意的是输出要回溯,不然容易错。

 1 /*************************************************************************
 2  
 3     > File Name: i.cpp
 4     > Author: Nature
 5     > Mail: 564374850@qq.com 
 6     > Created Time: Mon 04 Aug 2014 01:50:06 PM CST
 7 ************************************************************************/
 8 
 9 #include <cstdio>
10 #include <cstring>
11 #include <cstdlib>
12 #include <cmath>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 char s[1005];
18 int dp[1005][1005];
19 int len;
20 
21 void Print(int x,int y){
22     if(x > y)
23         return;
24     if(x == y){
25         printf("%c",s[x]);
26         return;
27     }
28     if(s[x] == s[y]){
29         printf("%c",s[x]);
30         Print(x + 1,y - 1);
31         printf("%c",s[y]);
32     }
33     else{
34         if(dp[x][y] == dp[x + 1][y] + 1){
35             printf("%c",s[x]);
36             Print(x + 1,y);
37             printf("%c",s[x]);
38         }
39         else{
40             printf("%c",s[y]);
41             Print(x,y - 1);
42             printf("%c",s[y]);
43         }
44     }
45 }
46 
47 int main(){
48     while(scanf("%s",s + 1) == 1){
49         len = strlen(s + 1); 
50         memset(dp,0,sizeof(dp));
51         for(int i = len; i >= 1; --i){
52             for(int j = i + 1; j <= len; ++j){
53                 if(s[i] != s[j]){
54                     if(dp[i + 1][j] < dp[i][j - 1]){
55                         dp[i][j] = dp[i + 1][j] + 1;
56                     }
57                     else{
58                         dp[i][j] = dp[i][j - 1] + 1;
59                     }
60                 }
61                 else
62                     dp[i][j] = dp[i + 1][j - 1];
63             }
64         }
65         printf("%d ",dp[1][len]);
66         Print(1,len);
67         printf("\n");
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3891343.html

【从高压输电线的架空地线中汲取电能】一个25千瓦受控电源从735千伏线路的架空地线中汲取电能的SimPowerSystems模型(Simulink仿真实现)内容概要:本文介绍了一个基于SimPowerSystems的Simulink仿真模型,用于模拟从735千伏高压输电线的架空地线中汲取25千瓦电能的受控电源系统。该模型聚焦于高压输电线路中架空地线的能量回收技术,通过仿真手段实现对电能采集过程的建模与控制策略验证,体现了电力系统中新型能源获取方式的技术可行性与工程应用潜力。文中还提及该资源属于一系列电力系统仿真研究的一部分,涵盖微电网、储能优化、碳流追踪、鲁棒调度等多个前沿方向,配套提供Matlab/Simulink代码及网盘资料链接,便于科研人员复现与拓展研究。; 适合人群:具备电力系统基础知识、熟悉Matlab/Simulink仿真环境,从事电力工程、能源回收或智能电网相关研究的科研人员及研究生;有一定编程与建模仿真经验的高年级本科生或工程技术人员。; 使用场景及目标:①研究高压输电线路中架空地线的能量回收机制与建模方法;②掌握基于Simulink的电力系统仿真技术,特别是受控电源与电网交互的态特性分析;③为开展能源 harvesting、分布式供能、电力电子变换器控制等相关课题提供参考模型与技术支撑; 阅读建议:建议结合提供的仿真模型文件进行实操演练,重点理解系统结构设计、参数设置与控制逻辑实现;同时可延伸学习文档中提到的其他电力系统优化与仿真案例,以拓宽研究视野和技术积累。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值