NEERC2018J(枚举+背包DP)

链接:http://codeforces.com/contest/1070

题意:给定一些字母,要求用这些字母填满长度为n和长度为m的字符串,这2个字符串的价值为对这2个字符串中的所有相同字母在这2个串中出现次数的乘积的和(比如:aabb和abb的价值为2*1+2*2=6)

这个其实可以发现产生价值的只有一个字母,而这个字母只有26个,所以可以枚举这个字母,枚举之后做个背包DP,这样枚举背包中每个可以到达的状态,我们可以把这些字母分成2个不含相同字母的串,然后用枚举出来的产生价值的字母去填充就可以了。。

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 200005
#define nm 20005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}










int n,m,tot,a[30],ans;
char _s[NM];
bool v[NM];


int main(){
    int _=read();while(_--){
	mem(a);
	n=read();m=read();tot=read();
	scanf("%s",_s+1);
	inc(i,1,tot)a[_s[i]-'A'+1]++;
	ans=inf;
	inc(k,1,26){
	    v[0]++;
	    inc(i,1,tot)v[i]=false;
	    inc(i,1,26)if(i!=k)dec(j,tot,a[i])if(v[j-a[i]])v[j]++;
	    inc(i,max(0,n-a[k]),n)if(v[i]){
		int t=m-(tot-i-a[k]);
		t=max(t,0);
		ans=min(ans,t*(n-i));
	    }
	}
	printf("%d\n",ans);
    }
}

 

.

 

 

J. Streets and Avenues in Berhattan

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Berhattan is the capital of Berland. There are n

streets running parallel in the east-west direction (horizontally), and there are m avenues running parallel in the south-north direction (vertically). Each street intersects with each avenue, forming a crossroad. So in total there are n⋅m

crossroads in Berhattan.

Recently, the government has changed in Berland. The new government wants to name all avenues and all streets after heroes of revolution.

The special committee prepared the list of k

names. Only these names can be used as new names for streets and avenues. Each name can be used at most once.

The members of committee want to name streets and avenues in the way that minimizes inconvenience for residents. They believe that if street and avenue names start with the same letter, then their crossroad will be inconvenient. Hence only the first letter of each name matters.

Given first letters of k

names, find C

— minimal possible number of inconvenient crossroads in Berhattan after the naming process.

Input

Input contains one or several test cases to process. The first line contains t

(1≤t≤30000

) — the number of test cases. Solve test cases separately, test cases are completely independent and do not affect each other.

The description of t

test cases follows. Each test case starts with line with space-separated numbers n,m,k (1≤n,m≤30000; n+m≤k≤2⋅105

) — the number of streets, number of avenues and the number of names in the committee's list, respectively.

The the second line of each test case contains a string of k

uppercase English letters. i-th letter of the string is the first letter of i

-th name from the committee's list.

It's guaranteed that the sum of numbers n

from all test cases is not greater than 30000. Similarly, the sum of numbers m from all test cases is not greater than 30000. The sum of numbers k from all test cases is not greater than 2⋅105

.

Output

For each test case print single number C

in the separate line — minimal possible number of inconvenient crossroads in Berhattan after the naming process.

Examples

Input

Copy

2
2 3 9
EEZZEEZZZ
2 7 9
EEZZEEZZZ

Output

Copy

0
4

Input

Copy

2
4 4 8
CZBBCZBC
1 1 4
TTCT

Output

Copy

1
0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值