uva1509(暴力dfs)
来源:互联网 发布:下载会声会影软件 编辑:程序博客网 时间:2024/06/15 03:23
题意:
给出两个字符串,第一个字符串中的每个字符(只包括小写字母)最多可以变成k(k<=3)个字符(不一定是小写字母),相同字母要变成相同的字符,问由第一个串变成第二个串合不合法。
思路:
串的长度只有15,直接暴力,用dfs+回溯去枚举所有情况。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<map>#include<stack>#include<cmath>#include<cstdlib>#include<climits>#include<sstream>using namespace std;char st[100],en[100];char maps[26][4],tmp[4];int dfs(int k, int p1, int p2 ,int l1, int l2){ if(p1==l1&&p2==l2) return 1; if(p1==l1||p2==l2) return 0;for(int i=0;i<k;i++){if(p2+i<l2){memset(tmp,0,sizeof tmp);for(int j=0;j<=i;j++)tmp[j]=en[p2+j];int flag=0;if(strcmp(maps[st[p1]-'a'],"")==0){flag=1;strcpy(maps[st[p1]-'a'],tmp);}if(flag||strcmp(maps[st[p1]-'a'],tmp)==0){if(dfs(k,p1+1,p2+i+1,l1,l2))return 1;}if(flag)strcpy(maps[st[p1]-'a'],"");}}return 0;}int main(){ int t;scanf("%d",&t);while (t--) {int k;scanf("%d%s%s",&k,st,en);int len1=strlen(st);int len2=strlen(en); if(len2>3*len1) {cout<<0<<endl; continue; } for(int i=0;i<26;++i) strcpy(maps[i],"");int ans=dfs(k,0,0,len1,len2);cout<<ans<<endl; } return 0;}
0 0
- uva1509(暴力dfs)
- 搜索uva1509
- uva1509(回溯)
- 暴力dfs POJ1426
- UVA 185(暴力DFS)
- poj-1691-暴力DFS
- poj1753(dfs暴力枚举)
- poj2965(dfs暴力枚举)
- hdu 4739 dfs+暴力
- Codeforces9C dfs暴力
- BNUOJ 39423 暴力,DFS
- DFS 暴力 1007
- poj2965 DFS暴力枚举
- poj 2718 暴力dfs
- HDU 5706 暴力dfs
- HDU 5547 (DFS暴力)
- H - Sudoku (dfs暴力)
- poj 3050 dfs(暴力)
- java socket编程
- Codeforces 5E
- Largest Number
- UIButton用法大全
- NSIS制作安装
- uva1509(暴力dfs)
- 设计模式之抽象工厂
- 59Spiral Matrix II
- 页面跳转与重定向(之一)
- java设计模式之三 —— 观察者模式整理
- Codeforces Round #250 D - The Child and Sequence/[TYVJ3838] DQS和序列(by 帝江&Darkfalmes)
- 登录和记住密码关于SharedPreferences存储密码
- 《周鸿祎自述:我的互联网方法论》
- 使用iOS自带AAC编码器