HDU 5330 Route Statistics

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5330

题意:给出n个长度为m,并且只有012组成的串,两个串的距离为每一位相差的绝对值相加,问距离为0-2m的对数分别有几对

参考:http://blog.csdn.net/glqac/article/details/48971143

最后计算答案的时候,距离为0的话只能跟相同串的互相选择,不为零就是直接相乘

最后所有对数都算了两遍,所以要除2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int N=2e5+5;
int cnt[N],dp[2][N][25];
int f[20];
char s[20];
long long ans[25];
int now=0,pre=1;
int main()
{
    f[0]=1;
    for(int i=1;i<=11;i++)
        f[i]=f[i-1]*3;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            int sum=0;
            for(int j=0;j<m;j++)
                sum=sum*3+s[j]-‘0‘;
            cnt[sum]++;
        }
        memset(dp[now],0,sizeof(dp[now]));
        for(int i=0;i<f[m];i++)
            dp[now][i][0]=cnt[i];
        for(int i=0;i<m;i++)
        {
            swap(now,pre);
            memset(dp[now],0,sizeof(dp[now]));
            for(int j=0;j<f[m];j++)
                for(int k=0;k<=2*m;k++)
                if (dp[pre][j][k])
            {
                int tem=j/f[i]%3;
                for(int l=0;l<=2;l++)
                {
                    int t=j-(tem-l)*f[i];
                    dp[now][t][k+abs(tem-l)]+=dp[pre][j][k];
                }
            }
        }
        memset(ans,0,sizeof(ans));
        for(int i=0;i<f[m];i++)
        {
            if (!cnt[i]) continue;
            ans[0]+=1LL*cnt[i]*(cnt[i]-1);//dp[now][i][0]==cnt[i]
            for(int j=1;j<=2*m;j++)
                ans[j]+=1LL*cnt[i]*dp[now][i][j];
        }
        for(int i=0;i<=2*m;i++)
            printf("%lld\n",ans[i]/2LL);
    }
    return 0;
}

  

时间: 2024-12-11 17:24:51

HDU 5330 Route Statistics的相关文章

HDU 4240 Route Redundancy 一条流最大的路径

题目来源:HDU 4240 Route Redundancy 题意:求最大流与一条流最大的路径的比值 前者最大流求出 后者是每一条路的最小值再取大 思路:我用的是dinic 可以在DFS的时候在传递一个参数 表示当前增广路可以通过最大的流量 然后当x==t 到达汇点时 在取他们的最大值 #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <al

hdu 4240 Route Redundancy 最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets.each street in the city has a capacity,which is the minimum of the capcities of the streets along that route. The redundancy ratio from point A to poi

HDU 4240 Route Redundancy

Route Redundancy Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 424064-bit integer IO format: %I64d      Java class name: Main A city is made up exclusively of one-way steets.each street in the city has a ca

BestCoder Round #41

闲来无事打打BC,想必也是极好的,先来个flag:我要刷完所有的BC!! 题A hdu 5228 题意:给你五张牌,问你能够换最少的牌数实现同花顺. 题解:暴力乱搞,才五张牌,枚举所有组成同花顺的可能,然后匹配看还要补多少张即可. 1 /*zhen hao*/ 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define lson l, m, rt*2 6 #define rson m + 1, r, rt*2+1 7 #defin

Choose the best route HDU杭电2680【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend's home as soon as possible . Now give you a map of the city's tra

HDU - 1599 find the mincost route(Floyd求最小环)

find the mincost route Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个

hdu 1599 find the mincost route

http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200 5 using namespace std; 6 const int inf=1<<28; 7 8 int g[maxn][maxn],dis[maxn][maxn]; 9 int

hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2801    Accepted Submission(s): 1

Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend's home as soon as possible . Now give you a map of the city's tra