• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            hdu3531

            Match

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
            Total Submission(s): 918    Accepted Submission(s): 298


            Problem Description
            There is a matrix of size n*n whose elements are either 0 or 1. Now your task is to find out that given a matrix of size m*m whose elements are also 0 or 1 whether it is a sub-matrix of the previous matrix.
             

            Input
            There is a matrix of size n*n whose elements are either 0 or 1. Now your task is to find out that given a matrix of size m*m whose elements are also 0 or 1 whether it is a sub-matrix of the previous matrix.
             

            Output
            If the second matrix is a sub-matrix of the first one, print “Yes” on a single line. Otherwise print “No”.
             

            Sample Input
            4 2 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 0 3 2 0 0 1 0 1 0 1 0 0 0 0 1 1
             

            Sample Output
            Yes No

            好題,昨天剛學(xué)了rabin_karp ,還是字符串匹配算法 (求t在p中的位置 ,令m=strlen(t))
            其實(shí)就是hash嘛,
            根據(jù)horner法則【霍納法則】來hash
            horner法則百度去

            我們hash出了t的值
            然后我們要求出p中所有連續(xù)的長度為m的串的hash值
            這個過程我們可以用horner法則推出一個關(guān)系式,相鄰的兩個長度為m的串的hash值之間的關(guān)系
            這個自己推一下就好了,
            所以我們求下一個hash值時候可以通過關(guān)系式直接求出,常數(shù)時間,
            知道我們求出一個hash值與t的hash值相等,或者找不到

            效率不解釋了

            這個題硬搞可以卡時過
            然后我們可以用rk搞
            上面講的是rk是一維的,這個顯然是二維的
            怎么辦呢,我們可以壓縮成一維的啊

            不解釋,這個跟動歸中的最小子矩陣和,等有異曲同工之妙

            上個垃圾的rk代碼


            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>
            using namespace std;
            int getv(char x)
            {
                
            return (x-'0');
            }
            int rk(char t[],char p[],int d)
            {
                
            int n=strlen(t);
                
            int m=strlen(p);
                
            long long h=(long long )pow(double(d),m-1);
                
            int pp=0;
                
            int tt=0;
                
            for(int i=0;i<m;i++)
                {
                    pp
            =d*pp+getv(p[i]);
                    tt
            =d*tt+getv(t[i]);
                }
                
            for(int s=0;s<=n-m;s++)
                {
                    cout
            <<"pp,tt is"<<pp<<","<<tt<<endl;
                    
            if(pp==tt)
                        
            return s;
                    
            if (s<n-m)
                    {
                        tt
            =getv(t[s+m])+d*(tt-h*getv(t[s]));
                    }
                }
                
            return -1;
            }
            int main()
            {
                
            char set1[15]={"0123456789"};//串由0 - 9 組成
                char set2[30]={"abcdefghijklmnopqrstuvwxyz"};
                
            char p[10]={"2365"};
                
            char t[]={"258569236589780"};
                
            int d=10;
                
            int i=rk(t,p,d);
                printf(
            "pos %d\n",i);
                
            return 0;
            }

            code

            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>  
            #define maxn 1005
            #define mod 10003
            #define r 2
            using namespace std;
            int n,m;
            int a[maxn][maxn],b[maxn][maxn];
            int pb[maxn];
            int tmp[maxn];
            int tv;
            void ready()
            {
                pb[
            0]=1;
                
            for(int i=1; i<=1000; i++) pb[i]=2*pb[i-1% mod;
            }
            int main()
            {
                
            int i,j,p,q,ans;
                ready();
                
            while(scanf("%d%d",&n,&m)!=EOF)
                {
                    
            for(i=1; i<=n; i++)
                        
            for(j=1; j<=n; j++)
                            scanf(
            "%d",&a[i][j]);
                    
            for(i=1; i<=m; i++)
                        
            for(j=1; j<=m; j++)
                            scanf(
            "%d",&b[i][j]);
                    tv
            =0;
                    
            for(i=1; i<=m; i++)
                    {
                        tmp[i]
            =0;
                        
            for(j=1; j<=m; j++)
                            tmp[i]
            =(tmp[i]*r+b[j][i])% mod;
                        tv
            =(tv*r+tmp[i])%mod;
                    }
                    memset(tmp,
            0,sizeof(tmp));
                    
            for(j=1; j<=n; j++)
                    {
                        tmp[j]
            =0;
                        
            for(i=1; i<=m; i++)
                            tmp[j]
            =(tmp[j]*r+a[i][j])%mod;
                    }
                    
            bool flag;
                    flag
            =0;
                    
            for(i=m; i<=n&&!flag; i++)
                    {
                        ans
            =0;
                        
            for(j=1; j<=n&&!flag; j++)
                            
            if(j<m)
                            {
                                ans
            =(ans*r+tmp[j])%mod;
                            }
                            
            else
                            {
                                ans
            =((ans-tmp[j-m]*pb[m-1])*r+tmp[j])%mod;
                                
            if((ans+mod)%mod==tv)
                                {
                                    
            if(a[i][j]==b[m][m]&&a[i][j-m+1]==b[m][1]&&a[i-m+1][j]==b[1][m]&&a[i-m+1][j-m+1]==b[1][1])
                                        flag
            =1;
                                    
            else flag=0;
                                    
            for( p=1; p<=m&&flag; p++)
                                        
            for(q=1; q<=m&&flag; q++)
                                        {
                                            
            if(b[p][q]!=a[i-m+p][j-m+q])
                                            {
                                                flag
            =0;
                                                
            break;
                                            }
                                            
            if(flag==0break;
                                        }
                                }
                            }
                        
            if(i<n)
                        {
                            
            for(j=1; j<=n; j++)
                                tmp[j]
            =((tmp[j]-a[i-m+1][j]*pb[m-1])*r+a[i+1][j])%mod;
                        }
                    }
                    
            if(flag)
                        printf(
            "Yes\n");
                    
            else printf("No\n");
                }
                
            return 0;
            }



            posted on 2012-07-19 23:21 jh818012 閱讀(107) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2012年9月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當(dāng)于是 取余3的意思 因?yàn)?3 的 二進(jìn)制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
            • --王私江
            麻豆精品久久久久久久99蜜桃| 久久免费小视频| 久久精品国产精品亚洲人人| 99精品久久久久中文字幕| 亚洲精品无码久久一线| 久久91精品国产91| 99久久做夜夜爱天天做精品| 久久久久久免费视频| 久久亚洲精品国产精品婷婷| 久久婷婷是五月综合色狠狠| 国产香蕉久久精品综合网| 一本久久a久久精品综合香蕉| 亚洲国产小视频精品久久久三级| 久久久久黑人强伦姧人妻| 久久久噜噜噜久久| 中文精品久久久久人妻| 久久伊人五月丁香狠狠色| 亚洲av伊人久久综合密臀性色| 性做久久久久久久| 国内精品久久久久影院一蜜桃| 国产精品久久成人影院| 国产精品美女久久久网AV| 色偷偷88欧美精品久久久| 思思久久精品在热线热| www性久久久com| 99久久国产主播综合精品| 久久综合成人网| 亚洲AV无码成人网站久久精品大| 99久久精品毛片免费播放| 久久WWW免费人成—看片| 伊人久久大香线蕉精品不卡| 亚洲熟妇无码另类久久久| 久久久久夜夜夜精品国产| 日产久久强奸免费的看| 久久精品国产亚洲AV无码偷窥| 99国内精品久久久久久久| 一本一道久久综合狠狠老| 成人午夜精品久久久久久久小说| 久久丫忘忧草产品| 久久国产成人午夜AV影院| 成人综合伊人五月婷久久|