UVa 10066 Twin Towers (DP 最长公共子序列)

题意  求两串数字最长公共子序列的长度

裸的lcs没啥说的

#include<cstdio>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
const int maxn=105;  
int a[maxn],b[maxn],d[maxn][maxn],na,nb;  
void lcs()  
{  
    memset(d,0,sizeof(d));  
    for(int i=1; i<=na; ++i)  
        for(int j=1; j<=nb; ++j)  
            if(a[i]==b[j]) d[i][j]=d[i-1][j-1]+1;  
            else d[i][j]=max(d[i-1][j],d[i][j-1]);  
}  
  
int main()  
{  
    int k=1;  
    while(scanf("%d%d",&na,&nb),na||nb)  
    {  
        for(int i=1; i<=na; ++i)  
            scanf("%d",&a[i]);  
        for(int i=1; i<=nb; ++i)  
            scanf("%d",&b[i]);  
        lcs();  
        printf("Twin Towers #%d\nNumber of Tiles : %d\n\n",k++,d[na][nb]);  
  
    }  
    return 0;  
}  

The Twin Towers

Input: standard input

Output: standard output

 

Once upon a time, in an ancient Empire, there were two towers of dissimilar shapes in two different cities. The towers were built by putting circular tiles one upon another. Each of the tiles was of the same height and had integral radius. It is no wonder that though the two towers were of dissimilar shape, they had many tiles in common.

However, more than thousand years after they were built, the Emperor ordered his architects to remove some of the tiles from the two towers so that they have exactly the same shape and size, and at the same time remain as high as possible. The order of the tiles in the new towers must remain the same as they were in the original towers. The Emperor thought that, in this way the two towers might be able to stand as the symbol of harmony and equality between the two cities. He decided to name them the Twin Towers.

Now, about two thousand years later, you are challenged with an even simpler problem: given the descriptions of two dissimilar towers you are asked only to find out the number of tiles in the highest twin towers that can be built from them.

 

 

Input

The input file consists of several data blocks. Each data block describes a pair of towers.

The first line of a data block contains two integers N1 and N2 (1 <= N1, N2 <= 100) indicating the number of tiles respectively in the two towers. The next line contains N1 positive integers giving the radii of the tiles (from top to bottom) in the first tower. Then follows another line containing N2 integers giving the radii of the tiles (from top to bottom) in the second tower.

The input file terminates with two zeros for N1 and N2.

 

Output

For each pair of towers in the input first output the twin tower number followed by the number of tiles (in one tower) in the highest possible twin towers that can be built from them. Print a blank line after the output of each data set.

 

Sample Input

7 6
20 15 10 15 25 20 15
15 25 10 20 15 20
8 9
10 20 20 10 20 10 20 10
20 10 20 10 10 20 10 10 20
0 0

 

Sample Output

Twin Towers #1
Number of Tiles : 4

Twin Towers #2
Number of Tiles : 6


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iom16v.h> #include <macros.h> #define RS_0 PORTD &= ~(1 << PD3) #define RS_1 PORTD |= (1 << PD3) #define RW_0 PORTD &= ~(1 << PD4) #define RW_1 PORTD |= (1 << PD4) #define EN_0 PORTD &= ~(1 << PD6) #define EN_1 PORTD |= (1 << PD6) //微秒级延时程序晶振8MHZ void delay_us(int time) { do { time--; } while (time>1); } //毫秒级延时程序晶振8MHZ void delay_ms(unsigned int time) { while(time!=0) { delay_us(1000); time--; } } /*显示屏命令写入函数*/ void LCD_write_com(unsigned char com) { RS_0; RW_0; PORTB = com; EN_1; delay_us(20); EN_0; } /*显示屏命令写入函数*/ void LCD_write_data(unsigned char data) { RS_1; RW_0; PORTB = data; EN_1; delay_us(200); EN_0; } /*显示屏清空显示*/ void LCD_clear(void) { LCD_write_com(0x01); delay_ms(5); } /*显示屏字符串写入函数*/ void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_write_com(0x80 + x); } else { LCD_write_com(0xC0 + x); } while (*s) { LCD_write_data( *s); s ++; } } /*显示屏单字符写入函数*/ void LCD_write_char(unsigned char x,unsigned char y,unsigned char data) { if (y == 0) { LCD_write_com(0x80 + x); } else { LCD_write_com(0xC0 + x); } LCD_write_data( data); } /*显示屏初始化函数*/ void LCD_init(void) { DDRB = 0xFF; /*I/O口方向设置*/ DDRD |= (1 << PD3) | (1 << PD4) | (1 << PD6); LCD_write_com(0x38); /*显示模式设置*/ delay_ms(5); LCD_write_com(0x38); delay_ms(5); LCD_write_com(0x38); delay_ms(5); LCD_write_com(0x38); LCD_write_com(0x08); /*显示关闭*/ LCD_write_com(0x01); /*显示清屏*/ LCD_write_com(0x06); /*显示光标移动设置*/ delay_ms(5); LCD_write_com(0x0C); /*显示开及光标设置*/ } void main(void) { unsigned char i; unsigned char *p; PORTA = 0xFF; /*打开上拉*/ DDRA = 0x00; /*方向输入*/ PORTB = 0xFF; /*电平设置*/ DDRB = 0xFF; /*方向输出*/ PORTC = 0x7F; DDRC = 0x80; PORTD = 0xFF; DDRD = 0x00; delay_ms(100); LCD_init(); while (1) { i = 1; p = "yixiangongren"; //LCD_clear(); LCD_write_str(1,0,"www.eehome.cn"); delay_ms(50); while (*p) { LCD_write_char(i,1,*p); i ++; p ++; //delay_ms(50); } delay_ms(500); } } 本文来自: 电工程师之家http://www.eehome.cn

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值