Strings - UVa 11081 dp

本文介绍了一个编程问题,即如何计算由两个字符串的子序列构成第三个特定字符串的不同方式的数量,并提供了一段AC代码作为解决方案。

Problem H
Strings
Input: 
Standard Input

Output: Standard Output

 

Given 3 strings of only lowercase letter you have to count the number of ways you can construct the third string by combining two subsequences from the first two strings.

           

After deleting 0 or more characters from a string we can get its subsequence. For example “a”, “b”, “c”, “ab”, “ac”, “bc” and “abc” all the strings are the subsequences of “abc”. A subsequence may also be empty.

 

Now suppose there are two subsequences “abc” and “de”. By combining them you can get the following strings  “abcde”, “abdce”, “abdec”, “adbce”, “adbec”, “adebc”, “dabce”, “dabec”, “daebc” and “deabc”.

 

Input

The first line of the input contains a single integer T (0<T<271) indicating the number of test cases.  Each test case contains 3 strings containing only lowercase characters. The lengths of the strings are between 1 and 60.

 

Output

For each test case output a single integer denoting the number of ways you can construct the third string from the first two string by the above way. The result may be very large. You should output the result%10007.

 

Sample Input                             Output for Sample Input

2

abc abc abc

abbcd bccde abcde

 

8

18

 



题意:由前两个字符串的子串组成第三个字符串,问有多少种方法。

思路:f1[i][j][k]表示第k个是从第一个字符串取的方法,f2[i][j][k]表示第k个是从第二个字符串取的方法,f表示f1和f2对应的和,然后递推。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[110],s2[110],s3[110];
int f1[110][110][110],f2[110][110][110],f[110][110][110];
int main()
{ int t,i,j,k,len1,len2,len3;
  scanf("%d",&t);
  while(t--)
  { scanf("%s%s%s",s1+1,s2+1,s3+1);
    len1=strlen(s1+1);
    len2=strlen(s2+1);
    len3=strlen(s3+1);
    memset(f1,0,sizeof(f1));
    memset(f2,0,sizeof(f2));
    memset(f,0,sizeof(f));
    for(i=0;i<=len1;i++)
     for(j=0;j<=len2;j++)
     { f1[i][j][0]=1;
       f2[i][j][0]=1;
       f[i][j][0]=1;
     }
    for(k=1;k<=len3;k++)
     for(i=0;i<=len1;i++)
      for(j=0;j<=len2;j++)
      { if(i==0 && j==0)
         continue;
        if(i>0)
        { f1[i][j][k]=f1[i-1][j][k];
          if(s1[i]==s3[k])
           f1[i][j][k]+=f[i-1][j][k-1];
        }
        if(j>0)
        { f2[i][j][k]=f2[i][j-1][k];
          if(s2[j]==s3[k])
           f2[i][j][k]+=f[i][j-1][k-1];
        }
        f[i][j][k]=(f1[i][j][k]+f2[i][j][k])%10007;
      }
    printf("%d\n",f[len1][len2][len3]);
  }
}



### 使用 `strings` 命令的 `-f` 选项 在 Linux 系统中,`strings` 命令用于从二进制文件或其他非文本文件中提取可打印的字符串。`-f` 选项会为每个输出的字符串附加文件名[^3]。这在处理多个文件时非常有用,可以清楚地知道每个字符串来自哪个文件。 以下是使用 `strings` 命令及其 `-f` 选项的详细说明和示例: #### 基本语法 ```bash strings [选项] 文件名 ``` #### `-f` 选项的用法 当指定 `-f` 选项时,`strings` 会在每个字符串前打印文件名。如果需要更紧凑的输出,可以结合 `-n` 选项来设置最小字符串长度。 #### 示例代码 以下是一个简单的示例,展示如何使用 `-f` 选项: ```bash # 创建一个包含可打印字符串的二进制文件 echo "Hello, World!" > example.bin # 使用 strings 命令并启用 -f 选项 strings -f example.bin ``` 运行上述命令后,输出将类似于以下内容: ``` example.bin: Hello, World! ``` #### 结合其他选项 可以与其他选项一起使用以增强功能。例如,使用 `-n` 指定最小字符串长度: ```bash strings -f -n 5 example.bin ``` 此命令只会输出长度至少为 5 的字符串,并附带文件名。 #### 注意事项 1. 如果没有指定文件名,`strings` 默认从标准输入读取数据。 2. 对于大型文件或目录中的多个文件,建议结合 `find` 或 `xargs` 命令使用。 ```bash find /path/to/files -type f -exec strings -f {} \; ``` 此命令会递归查找指定路径下的所有文件,并对每个文件运行 `strings -f`。 ### 相关工具 `strings` 命令通常与 `grep` 配合使用,以便过滤出特定的字符串[^3]。例如: ```bash strings -f /path/to/binary | grep "specific-pattern" ``` 此命令会从二进制文件中提取所有字符串,并仅显示包含 `"specific-pattern"` 的行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值