Combine String

Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. 
A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read them as a string, one equals to aa, and the other equals to bb. 
For example, ``adebcf'' is a combine string of ``abc'' and ``def''. 

Input

Input file contains several test cases (no more than 20). Process to the end of file. 
Each test case contains three strings aa, bb and cc (the length of each string is between 1 and 2000). 

Output

For each test case, print ``Yes'', if cc is a combine string of aa and bb, otherwise print ``No''. 

Sample Input

abc
def
adebcf
abc
def
abecdf

Sample Output

Yes
No

题意:给a,b,c三个串,看c,能否由a,b构成

思路:dp[i][j]表示c的前i+j个字符,能否由a的前i个,b的前j个构成,dp[0][0]=1(用1表示能构成)

状态转移:当a[i]==c[i+j]时,考虑dp[i-1][j],

                 当b[j]=c[i+j]时,考虑dp[i][j-1]

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
const int maxn=1e5+10;
using namespace std;
char a[maxn],b[maxn],s[maxn];
int dp[2005][2005];
int main(int argc, char const *argv[])
{
    while(scanf("%s%s%s",a,b,s)!=EOF)
    {
        int la=strlen(a),lb=strlen(b),ls=strlen(s);
        //memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<=la;i++)
        {
            for(int j=0;j<=lb;j++)
            {
                if(i && j) dp[i][j] = 0; 
                if(i)
                {
                    dp[i][j]=dp[i-1][j]&(a[i-1]==s[i+j-1]);//注意从0开始,需要减一
                }
                if(j)
                {
                    dp[i][j]|=dp[i][j-1]&(b[j-1]==s[i+j-1]);
                }
            }
        }
        if(dp[la][lb]==1&&la+lb==ls)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
    }
    return 0;
}

 

### Kotlin Flow Combine Usage In the context of Android development, combining multiple flows is a common requirement when dealing with state management or complex data streams. The `combine` operator allows merging two or more Flows into one single Flow that emits combined results whenever any upstream flow emits new values. The syntax for using `combine` involves passing each source Flow as an argument along with a lambda function defining how these emissions should be transformed together before being sent downstream[^2]. ```kotlin val searchQueryFlow = MutableStateFlow("") val categoryFilterFlow = MutableStateFlow("All") // Combining both flows to create filtered items list based on query and category. val filteredItemsFlow = combine(searchQueryFlow, categoryFilterFlow) { query, category -> repository.getItems().filter { it.name.contains(query, ignoreCase = true) && (category == "All" || it.category == category) } } ``` This code snippet demonstrates creating a combined flow from two separate sources (`searchQueryFlow`, `categoryFilterFlow`). Whenever either changes, this triggers recomputation according to specified logic within provided lambda expression which filters items accordingly. When working inside coroutine scope, ensure proper handling through structured concurrency principles by launching coroutines only where necessary while avoiding potential memory leaks due to unbounded collectors listening indefinitely without cancellation mechanisms in place[^1]. #### Example Implementation Using ViewModelScope To implement such functionality safely within an Android application's architecture component like ViewModels: ```kotlin class ItemViewModel : ViewModel() { private val _query = MutableStateFlow<String>("") private val _category = MutableStateFlow<String>("All") // Expose non-mutable version publicly so UI cannot modify directly but observe instead. val items: StateFlow<List<Item>> get() = _items.asStateFlow() init { viewModelScope.launch { combine(_query, _category) { q, c -> filterItems(q, c) } .collectLatest { itemList -> _items.value = itemList } } } fun updateSearchQuery(newText: String) { _query.value = newText } fun changeCategory(selectedCat: String) { _category.value = selectedCat } private suspend fun filterItems(query: String, cat: String): List<Item> = repository.getItems() .filter { item -> item.name.contains(query, ignoreCase = true) && (cat == "All" || item.category.equals(cat)) } } ``` Here `_query` and `_category` are mutable states representing user inputs; they're used alongside `combine()` method to produce updated lists reflecting current filtering criteria dynamically over time as users interact with interface elements tied back here via corresponding setter methods defined above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值