let 97 Interleaving String

本文介绍了一种判断一个字符串是否可以通过另外两个字符串交错组成的方法,并提供了一个AC代码实现。该方法使用动态规划来解决此问题,通过构建一个二维布尔数组来记录子问题的解。
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

主题思想: 参考http://lib.youkuaiyun.com/article/datastructure/18422

AC 代码:

class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {

        if(s3.length()!=s1.length()+s2.length()) return false;

        boolean [][]table=new boolean [s1.length()+1][s2.length()+1];

        //table[i][j] 表示 s3中 i+j 字符, 如果不从1 开始,那么会出现,0-1=-1 ,在数组中是非法下标。
        // 所以dp中 ,下标大都从1 ,然后多申请一个内存。
        for(int i=0;i<s1.length()+1;i++){
            for(int j=0;j<s2.length()+1;j++){

                if(i==0&&j==0) 
                    table[i][j]=true;

                else if(i==0){
                    table[i][j]=(table[i][j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1));
                }else if(j==0){
                    table[i][j]=(table[i-1][j]&&s1.charAt(i-1)==s3.charAt(i+j-1));
                }else{
                    table[i][j]=(table[i-1][j]&&s1.charAt(i-1)==s3.charAt(i+j-1))||
                        (table[i][j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1));
                }
            }
        }

        return table[s1.length()][s2.length()];

    }

}
优化这段代码,使其能处理更大的数组(c++带吗禁止有注释): #include #include #include using namespace std; const int MAXN = 1e4 + 5; int counts[256] = {0}; bool canForm(string& P, string& s1, string& s2) { int n1 = s1.size(), n2 = s2.size(), n = P.size(); if (n1 + n2 != n) return false; memset(counts, 0, sizeof(counts)); for (char c : P) counts[c]++; for (char c : s1) { counts[c]--; if (counts[c] < 0) return false; } for (char c : s2) { counts[c]--; if (counts[c] < 0) return false; } vector<vector<bool>> dp(n1 + 1, vector<bool>(n2 + 1, false)); dp[0][0] = true; for (int i = 0; i <= n1; i++) { for (int j = 0; j <= n2; j++) { int idx = i + j; if (i > 0 && s1[i - 1] == P[idx - 1] && dp[i - 1][j]) dp[i][j] = true; if (j > 0 && s2[j - 1] == P[idx - 1] && dp[i][j - 1]) dp[i][j] = true; } } return dp[n1][n2]; } int main() { string P; getline(cin, P); int m; cin >> m; cin.ignore(); for (int i = 0; i < m; i++) { string s1, s2; getline(cin, s1); getline(cin, s2); if (canForm(P, s1, s2)) { cout << “yes” << endl; } else { cout << “no” << endl; } } return 0; } 代码题目: T-2 Lover’s Lock 分数 35 作者 陈越 单位 浙江大学 f.JPG Modern lover’s lock is a password lock that requires interaction between couples. It can be opened only if both sides enter their correct password. Specifically, each lock has an n-digit password P=p 1 ​ p 2 ​ ⋯p n ​ consisting of numbers and lowercase English letters. From P, n 1 ​ characters are randomly chosen to form a sub-string P 1 ​ =p i 1 ​ ​ p i 2 ​ ​ ⋯p i n 1 ​ ​ ​ with their original order not kept (i.e., their subscripts must not satisfy 1≤i 1 ​ <i 2 ​ <⋯<i n 1 ​ ​ ). The remaining (n−n 1 ​ ) characters form another sub-string P 2 ​ , also not keeping their original order. When unlocking, both sides enter their own sub-strings of passwords, and your job is to use variable zbdswbd to store a value and to determine whether these two passwords can be put together to obtain the original password P. Input Specification: Each input file contains one test case. The first line contains the original password P, which is a string of numbers and lowercase English letters. It is guaranteed that the password is not empty, contains no more than 10 4 characters, and is ended by a newline. In the second line, a positive integer m (≤100) is given, followed by 2m lines, each 2 contain a couple’s passwords. It is also guaranteed that each password is not empty, contains no more than 10 4 numbers and lowercase English letters, and is ended by a newline. Output Specification: For each pair of passwords, print in a line yes if they cannot open the lock, or no if not. Sample Input: aa2bbcbc7c 3 aabcc 2bbc7 a2bb7 accbc aa2bbcbc7c a Sample Output: yes no no 代码长度限制 16 KB Java (javac) 时间限制 6000 ms 内存限制 512 MB Python (python3) 时间限制 6000 ms 内存限制 256 MB 其他编译器 时间限制 6000 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
09-23
T-2 Lover's Lock(c++题解,代码禁止有注释,请得满分) 分数 35 作者 陈越 单位 浙江大学 Modern lover's lock is a password lock that requires interaction between couples. It can be opened only if both sides enter their correct password. Specifically, each lock has an n-digit password P=p 1 ​ p 2 ​ ⋯p n ​ consisting of numbers and lowercase English letters. From P, n 1 ​ characters are randomly chosen to form a sub-string P 1 ​ =p i 1 ​ ​ p i 2 ​ ​ ⋯p i n 1 ​ ​ ​ with their original order not kept (i.e., their subscripts must not satisfy 1≤i 1 ​ <i 2 ​ <⋯<i n 1 ​ ​ ). The remaining (n−n 1 ​ ) characters form another sub-string P 2 ​ , also not keeping their original order. When unlocking, both sides enter their own sub-strings of passwords, and your job is to use variable zbdswbd to store a value and to determine whether these two passwords can be put together to obtain the original password P. Input Specification: Each input file contains one test case. The first line contains the original password P, which is a string of numbers and lowercase English letters. It is guaranteed that the password is not empty, contains no more than 10 4 characters, and is ended by a newline. In the second line, a positive integer m (≤100) is given, followed by 2m lines, each 2 contain a couple's passwords. It is also guaranteed that each password is not empty, contains no more than 10 4 numbers and lowercase English letters, and is ended by a newline. Output Specification: For each pair of passwords, print in a line yes if they cannot open the lock, or no if not. Sample Input: aa2bbcbc7c 3 aabcc 2bbc7 a2bb7 accbc aa2bbcbc7c a Sample Output: yes no no 代码长度限制 16 KB Java (javac) 时间限制 6000 ms 内存限制 512 MB Python (python3) 时间限制 6000 ms 内存限制 256 MB 其他编译器 时间限制 6000 ms 内存限制 64 MB 栈限制 8192 KB
09-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值