思路是枚举矩阵列数,然后将字符矩阵转换成字符串,通过字符数组求不同子串数目。最后,减去不成立的情况。使用特殊字符分割可能的组合。

 /* 4029 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxr = ;
const int maxn = 2e5+;
const int base = ;
map<int,int> tb;
int a[maxn*];
int height[maxn], rrank[maxn], sa[maxn*];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
char s[maxr][maxr];
int H[maxr][maxr];
int r, c; bool c0(int *r, int a, int b) {
return r[a]==r[b] && r[a+]==r[b+] && r[a+]==r[b+];
} bool c12(int k, int *r, int a, int b) {
if (k == )
return r[a]<r[b] || (r[a]==r[b] && c12(, r, a+, b+));
else
return r[a]<r[b] || (r[a]==r[b] && wv[a+]<wv[b+]);
} void sort(int *r, int *a, int *b, int n, int m) {
int i; for (i=; i<n; ++i) wv[i] = r[a[i]];
for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[wv[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) b[--wc[wv[i]]] = a[i];
} #define F(x) ((x)/3 + ((x)%3==1 ? 0:tb))
#define G(x) ((x)<tb ? (x)*3+1 : ((x)-tb)*3+2)
void dc3(int *r, int *sa, int n, int m) {
int i, j, *rn=r+n, *san=sa+n, ta=, tb=(n+)/, tbc=, p; r[n] = r[n+] = ;
for (i=; i<n; ++i) if (i%!=) wa[tbc++] = i;
sort(r+, wa, wb, tbc, m);
sort(r+, wb, wa, tbc, m);
sort(r, wa, wb, tbc, m);
for (p=, rn[F(wb[])]=, i=; i<tbc; ++i)
rn[F(wb[i])] = c0(r, wb[i-], wb[i]) ? p- : p++;
if (p < tbc)
dc3(rn, san, tbc, p);
else
for (i=; i<tbc; ++i) san[rn[i]] = i;
for (i=; i<tbc; ++i)
if (san[i] < tb)
wb[ta++] = san[i] * ;
if (n% == )
wb[ta++] = n - ;
sort(r, wb, wa, ta, m);
for (i=; i<tbc; ++i) wv[wb[i]=G(san[i])] = i;
for (i=,j=,p=; i<ta && j<tbc; ++p)
sa[p] = c12(wb[j]%, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
while (i < ta) sa[p++] = wa[i++];
while (j < tbc) sa[p++] = wb[j++];
} void calheight(int *r, int *sa, int n) {
int i, j, k = ; for (i=; i<=n; ++i) rrank[sa[i]] = i;
for (i=; i<n; height[rrank[i++]]=k)
for (k?k--:, j=sa[rrank[i]-]; r[j+k]==r[i+k]; ++k) ;
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} void solve() {
__int64 ans = , tot, tmp;
int n = ; memset(H, , sizeof(H));
rep(w, , c+) {
tb.clr();
int cn = c - w + ;
int cnt = cn + ;
int sidx = ; rep(i, , r) {
rep(j, , cn) {
H[i][j] = H[i][j] * base + s[i][j+w-]-'A';
if (tb.find(H[i][j]) == tb.end())
tb[H[i][j]] = cnt++;
}
} n = ;
rep(j, , cn) {
rep(i, , r)
a[n++] = tb[H[i][j]];
a[n++] = sidx++;
}
a[n] = ; dc3(a, sa, n+, cnt+);
calheight(a, sa, n); tot = ;
rep(i, , n+)
tot += (n - sa[i] - height[i]); tmp = n;
rep(i, , cn) {
tmp -= r;
tot -= (r+) * tmp;
--tmp;
} ans += tot;
} printf("%I64d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d %d", &r, &c);
rep(i, , r)
scanf("%s", s[i]);
printf("Case #%d: ", tt);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据生成器。

 from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 20
uc = list(string.uppercase)
fout.write("%d\n" % (t))
for tt in xrange(t):
n = randint(20, 50)
m = randint(20, 50)
fout.write("%d %d\n" % (n, m))
for i in xrange(n):
line = ""
for j in xrange(m):
idx = randint(0, 25)
line += uc[idx]
fout.write("%s\n" % (line)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【HDOJ】4029 Distinct Sub-matrix的更多相关文章

  1. 【线性代数】2-4:矩阵操作(Matrix Operations)

    title: [线性代数]2-4:矩阵操作(Matrix Operations) toc: true categories: Mathematic Linear Algebra date: 2017- ...

  2. 【HDOJ】2830 Matrix Swapping II

    简单DP. /* 2830 */ #include <iostream> #include <string> #include <map> #include < ...

  3. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  4. 【Lintcode】118.Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  5. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  6. 【leetcode】 Search a 2D Matrix (easy)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  8. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  9. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

随机推荐

  1. 使用分部类给Models添加验证Attributes

    网摘1: 在使用Entity Framework 的Database frist或model first时,直接加attribute到modle类上是太现实也不合理的,因为model类是自动生成的,重 ...

  2. TreeView递归取值

    string jingyuan = ""; string jinghui = ""; private void DiGui(TreeNode tn) { if ...

  3. 将XML文件保存到DataGridView中

    #region get护理单记录信息XML //将XML文件保存到DataTable private DataTable FromXML2DataTable(string XMLStr,string ...

  4. php array_walk 和 array_reduce函数

    1.array_walk:将数组中的元素(键+值)依次取出传给处理的函数,函数处理完就完了,没有返回值. $arr1=array( 'name'=>'zhangsan', 'age'=>3 ...

  5. makefile:4: *** missing separator. Stop.

    今天在编写蜂鸣器的驱动程序时,makefile文件是这样: CROSS=arm-linux- all: beep beep: beep.c $(CROSS)gcc -o beep beep.c $(C ...

  6. asp.net web form中 用attribute实现权限验证方式

    以前项目的代码比较陈旧,今天抽空优化了一下.作为记录. 以前每次请求一个方法都要验证是否登录 if xxx等  现在通过global文件中的改进 反射这个方法的属性是否需要权限 要的话先验证权限.以下 ...

  7. (转)PHP中的 抽象类(abstract class)和 接口(interface)

    转自:http://blog.csdn.net/sunlylorn/article/details/6124319 一. 抽象类abstract class 1 .抽象类是指在 class 前加了 a ...

  8. ASP.NET MVC 简易在线书店

    写这篇博客的目的是为了记录自己的思想,有时候做项目做着做着就不知道下面该做什么了,把项目的具体流程记录下来,培养好习惯. 创建MVC项目 创建控制器StoreController public cla ...

  9. POJ 2411 压缩状态DP

    这个题目非常赞! 给定一个矩形,要求用1*2 的格子进行覆盖,有多少种覆盖方法呢? dp[i][j] 当状态为j,且第i行已经完全铺满的情况下的种类数有多少种?j中1表示占了,0表示没有被占. 很显然 ...

  10. iOS人脸识别核心代码(备用)

    for (int i = 0; i < 1; i++) { //< [arr count]; i++) { CIFaceFeature *feature = [arr objectAtIn ...