ACM練習中關於LCS的題目

来源:https://www.cnblogs.com/kingofnight/archive/2018/03/27/8661021.html
-Advertisement-
Play Games

You are planning to take some rest and to go out on vacation, but you really don’t know which cities you should visit. So, you ask your parents for he ...


You are planning to take some rest and to go out on vacation, but you really don’t know which cities you should visit. So, you ask your parents for help. Your mother says “My son, you MUST visit Paris, Madrid, Lisboa and London. But it’s only fun in this order.” Then your father says: “Son, if you’re planning to travel, go first to Paris, then to Lisboa, then to London and then, at last, go to Madrid. I know what I’m talking about.”

  Now you’re a bit confused, as you didn’t expected this situation. You’re afraid that you’ll hurt your mother if you follow your father’s suggestion. But you’re also afraid to hurt your father if you follow you mother’s suggestion. But it can get worse, because you can hurt both of them if you simply ignore their suggestions!

  Thus, you decide that you’ll try to follow their suggestions in the better way that you can. So, you realize that the “Paris-Lisboa-London” order is the one which better satisfies both your mother and your father. Afterwards you can say that you could not visit Madrid, even though you would’ve liked it very much.

  If your father have suggested the “London-Paris-Lisboa-Madrid” order, then you would have two orders, “Paris-Lisboa” and “Paris-Madrid”, that would better satisfy both of your parent’s suggestions. In this case, you could only visit 2 cities.

  You want to avoid problems like this one in the future. And what if their travel suggestions were bigger? Probably you would not find the better way very easy. So, you decided to write a program to help you in this task. You’ll represent each city by one character, using uppercase letters, lowercase letters, digits and the space. Thus, you can have at most 63 different cities to visit. But it’s possible that you’ll visit some city more than once.

  If you represent Paris with ‘a’, Madrid with ‘b’, Lisboa with ‘c’ and London with ‘d’, then your mother’s suggestion would be ‘abcd’ and you father’s suggestion would be ‘acdb’ (or ‘dacb’, in the second example).

  The program will read two travel sequences and it must answer how many cities you can travel to such that you’ll satisfy both of your parents and it’s maximum.

Input

The input will consist on an arbitrary number of city sequence pairs. The end of input occurs when the first sequence starts with an ‘#’ character (without the quotes). Your program should not process this case. Each travel sequence will be on a line alone and will be formed by legal characters (as defined above). All travel sequences will appear in a single line and will have at most 100 cities.

Output

For each sequence pair, you must print the following message in a line alone:

Case #d: you can visit at most K cities.

  Where d stands for the test case number (starting from 1) and K is the maximum number of cities you can visit such that you’ll satisfy both you father’s suggestion and you mother’s suggestion.

Sample Input

abcd

acdb

abcd

dacb

#

Sample Output

Case #1: you can visit at most 3 cities.

Case #2: you can visit at most 2 cities.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

簡單的最長子序列匹配,演算法沒什麼難度,但是想要ac還需要註意的是程式的輸入和輸出,不要因為題目簡單而在最簡單的輸入輸出上栽跟頭,本題中輸入字元串c++必須使用getline(cin,str),否則ac就會失敗

ac代碼如下:

#include <iostream>
#include <cstring>

using namespace std;  
int d[110][110];  
int main()  
{  
    string a,b;  
    int i,j;
    int count=0;  
    while(getline(cin,a))  //必須使用getline 才能ac,cin輸入字元串將會錯誤
    {
        if(a=="#")
        {
            break;
        }
        getline(cin,b);
        memset(d,0,sizeof(d));  
        for(i=1; i<=a.size(); i++)  
            for(j=1; j<=b.size(); j++)  
            {  
                if(a[i-1] == b[j-1])  
                {  
                    d[i][j] = d[i-1][j-1]+1;  
                }     
                else  
                {  
                    d[i][j] = max(d[i-1][j],d[i][j-1]);  
                }  
            }  
        cout << "Case #"<<++count<<": you can visit at most "<<d[a.size()][b.size()]<<" cities." << endl;  
        a.clear();  
        b.clear();
    }  
    return 0;  
}  

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 月薪30K java程式員2018年java學習進階書籍推薦 ...
  • 內容:例子 ...
  • Hibernate框架 一、什麼是hibernate? Hibernate是一個開放源代碼的對象關係映射框架,它對JDBC進行了非常輕量級的對象封裝,它將POJO與資料庫表建立映射關係,是一個全自動的orm框架,hibernate可以自動生成SQL語句,自動執行,使得Java程式員可以隨心所欲的使用 ...
  • 2018-03-28 00:56:39 中斷正在執行的代碼 無論是%run執行的腳本還是長時間運行的命令ctrl + cIn [1]: KeyboardInterrupt 執行剪切板中的代碼 ctrl-shift-V %paste,%cpaste魔術函數%paste可以承載剪切板中的一切文本,併在s ...
  • 根據一個知乎高票回答的THU大神學習的 http://nbviewer.jupyter.org/github/lijin THU/notes python/tree/master/ 只整理了一些我認為不太熟悉的東西 對於acmer或者一些熟悉C++的童鞋來說應該有些參考吧,我覺得 我覺得python ...
  • 最近一直陷入一個誤區,老是找一些網上關於SSM速成等視頻學習,然後盲目的跟著'複製'代碼,當時跟著視頻敲完代碼,實現了某些功能後,感覺自己對Spring等一些框架已經有了足夠的瞭解(其實只是知其然,不知其所以然。) 過了一段時間,工作中用不到Spring,等到某天需要使用的時候,突然發現連手動搭建一 ...
  • 一.簡介 jxl是一個南韓人寫的java操作excel的工具, 在開源世界中,有兩套比較有影響的API可 供使用,一個是POI,一個是jExcelAPI。其中功能相對POI比較弱一點。但jExcelAPI對中文支持非常好,API是純Java的, 並不 依賴Windows系統,即使運行在Linux下, ...
  • pycharm中運行django預設情況下並不是執行項目的,所以如果在非manage.py,會發生異常。 raise AppRegistryNotReady("Apps aren't loaded yet.")django.core.exceptions.AppRegistryNotReady: A ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...