cf 154.div2 D. Table with Letters - 2

简介:

D. Table with Letters - 2

 

      今天的比赛题比较奸诈,居然是文件读入的,让A题耽误了很多时间。

   

      这题是dp的一个经典类型,o((n*m)^2)的算法非常好写,但是必然TLE。因为其要求四角均相等,不妨以此为条件进行判断,即可缩为o(n^2*m)算法

 

    /*
    author:jxy
    lang:C/C++
    university:China,Xidian University
    **If you need to reprint,please indicate the source**
    */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #define INF 1E9
    using namespace std;
    int sum[402][402];
    char a[402][402];
    int main()
    {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        int m,n,K,i,j,t;
        scanf("%d%d%d",&n,&m,&K);
        memset(sum,0,sizeof(sum));
        for(i=1;i<=n;i++)
        {
            getchar();
          for(j=1;j<=m;j++)
          {
              a[i][j]=getchar();
              sum[i][j]=0-sum[i-1][j-1]+sum[i-1][j]+sum[i][j-1];
              if(a[i][j]=='a')sum[i][j]++;
          }
        }
        long long ans=0;
        int now[257],k,p;
        for(i=1;i<=n;i++)
          for(j=i+1;j<=n;j++)
          {
              memset(now,0,sizeof(now));
              for(k=1,p=1;k<=m;k++)
              {
                  if(a[i][k]!=a[j][k])continue;
                  now[a[i][k]]--;
                 // if(p==1)p=k;
                  //cout<<now[a[i][k]]<<endl;
                  while(p<=m&&sum[j][p]+sum[i-1][k-1]-sum[i-1][p]-sum[j][k-1]<=K)
                  {
                      if(a[i][p]==a[j][p])now[a[i][p]]++;
                      p++;
                  }
                  if(now[a[i][k]]>0)ans+=now[a[i][k]];
                  //cout<<now[a[i][k]]<<" --------"<<endl;
              }
          }
        cout<<ans<<endl;
    }


 

目录
相关文章
|
6月前
|
索引 Python
row[i] = col[j] = TrueIndexError: list assignment index out of range
row[i] = col[j] = TrueIndexError: list assignment index out of range
../../..xxx.go:46:18: aa.Bbb undefined (type *"xx/xxx/xx".Ccc has no field or method Bbb)
../../..xxx.go:46:18: aa.Bbb undefined (type *"xx/xxx/xx".Ccc has no field or method Bbb)
|
机器学习/深度学习 人工智能
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)B. Take Your Places!
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)B. Take Your Places!
39 0
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
46 0
|
SQL 关系型数据库 MySQL
near ‘order values(‘1‘,‘1‘,‘100‘,‘10.25‘)‘ at line 1
near ‘order values(‘1‘,‘1‘,‘100‘,‘10.25‘)‘ at line 1
80 0
|
JSON fastjson 数据格式
报错:JSONException: illegal identifier : {pos 1, line 1, column 2{{&quot;group&quot;:&quot;trade_p0&qu
报错:JSONException: illegal identifier : {pos 1, line 1, column 2{{&quot;group&quot;:&quot;trade_p0&qu
566 0
|
SQL 关系型数据库 MySQL
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
278 0
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
when click one item in table Select at least one column to perform the search
when click one item in table Select at least one column to perform the search
114 0
when click one item in table Select at least one column to perform the search