UVA 1625 Color Length
来源:互联网 发布:苹果cms模板怎么用 编辑:程序博客网 时间:2024/04/30 21:32
题目链接:http://acm.hust.edu.cn/vjudge/problem/51186
题意:有两个字符串,现在用这两个字符串来构造一个更长的字符串C,规则是每次从这两个字符串中选一个非空串,将第一个字符拿走接到C后面。这样会得到lenA+lenB长度的串C,然后对于每个出现的字母,找到它最先出现的位置和最晚出现的位置作差。将所有的字母差加和就得到字符串C的sum,现在求sum最小可以是多少。
思路:参考书上的思想,dp[i][j]表示A串已经拿掉了i个字母,B串拿掉了j个字母。这时候如果再往后加一个字母的话,设前面i+j个字符里面有x个字母已经出现,但是还没结束,也就是说,现在多往后加一个字符,这x个字母的结束位置就会多往后拖一个,那么答案就会多加x。先预处理出来s[i][j],表示A串的前i个字母,B串的前j个字母中有多少个已经出现,但是还没结束的字符。dp[i][j] = min( dp[i-1][j] + s[i-1][j] , dp[i][j-1] + s[i][j-1] )。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 5009;int n,m;int dp[maxn][maxn];int s[maxn][maxn];char A[maxn];char B[maxn];int la,lb;int tot[30];//两个字符串中每个字母的总数void init(){ int tA[30],tB[30];//处理到当前位置A串中已经拿掉每个字母的数量和B串已经拿掉每个字母的数量 gets(A+1); gets(B+1); Clean(tot,0); Clean(tA,0); Clean(tB,0); la = strlen(A+1); lb = strlen(B+1); rep(i,1,la) tot[ A[i] - 'A' ]++; rep(i,1,lb) tot[ B[i] - 'A' ]++; int x = 0; if ( tot[ A[1] - 'A' ] > 1 ) s[1][0] = 1; if ( tot[ B[1] - 'A' ] > 1 ) s[0][1] = 1; rep(i,0,la) //预处理s数组 { Clean(tB,0); if ( i != 0 ) { tA[A[i]-'A']++; if ( tot[A[i]-'A'] > tA[A[i]-'A'] && tA[A[i]-'A'] == 1 ) x++; //某个字母第一次出现且总数不是1,也就是已经开始但未结束 else if ( tot[A[i]-'A'] == tA[A[i]-'A'] && tot[A[i]-'A'] != 1 ) x--;//当前的字母结束了 } int y = x; rep(j,0,lb) { if ( j != 0 ) { tB[B[j]-'A']++; if ( tot[B[j]-'A'] > tA[B[j]-'A'] + tB[B[j]-'A'] && tA[B[j]-'A'] + tB[B[j]-'A'] == 1 ) y++; else if ( tot[B[j]-'A'] == tA[B[j]-'A'] + tB[B[j]-'A'] && tot[B[j]-'A'] != 1 ) y--; } s[i][j] = y; } }}void solve(){ rep(i,1,la) dp[i][0] = dp[i-1][0] + s[i-1][0]; rep(i,1,lb) dp[0][i] = dp[0][i-1] + s[0][i-1]; rep(i,1,la) rep(j,1,lb) { dp[i][j] = min( dp[i-1][j] + s[i-1][j] , dp[i][j-1] + s[i][j-1] ); } printf("%d\n",dp[la][lb]);}int main(){ int T; cin>>T; getchar(); rep(i,1,T) { init(); solve(); } return 0;}
0 0
- Uva-1625-Color Length
- UVA 1625 Color Length
- UVA-1625 Color Length
- uva 1625 color length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVa 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- Color Length - UVa 1625 dp
- UVa OJ 1625 - Color Length
- uva 1625 Color Length (dp)
- UVa #1625 Color Length (例题9-8)
- UVA 1625 Color Length (DP)
- UVa 1625:Color Length(DP)
- uva 1625——Color Length
- Color Length UVA
- ionic 使用genymotion模拟器
- Java for Web学习笔记(二八):JSTL(4)Core Tag(下)
- AndroidAnnotations框架
- UIButton,UIScrollView,UITableView常见属性,使用案例 - iOS
- ArrayBlockingQueue源码分析及使用
- UVA 1625 Color Length
- UVA10055 Hashmat the Brave Warrior
- Java 中二维数组的长度是否固定?
- 设计模式-行为型- 策略模式(Strategy)
- Java并发编程:volatile关键字解析
- 排序算法之插入排序(模板类)
- "partition"和“stable_partition”的思考
- 10分钟快速了解Pandas
- hdoj 5615 Jam's math problem (数学-因式分解)