青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

TC-SRM-454_div2

Posted on 2009-12-13 10:18 rikisand 閱讀(323) 評論(0)  編輯 收藏 引用 所屬分類: TopcoderAlgorithm

200pt( 略)

500pt

Problem Statement

You are playing a game with a NxN grid of letters. The goal of the game is to spell out a N-letter word somewhere on the grid either horizontally from left to right or vertically from top to bottom. To achieve this, you will perform a series of moves. On each move, you can swap either two rows or two columns of the grid.
You are given a vector <string> grid containing the initial state of the grid. The j-th character of the i-th element of grid is the letter at row i, column j. The word you must spell is given to you in the string word. All letters in word are distinct. Note that lowercase and uppercase versions of the same letter are considered different in this problem, so 'A' and 'a' are distinct. Return the minimal number of moves required to spell the given word on the grid, or -1 if it is impossible.

Definition

Class:
WordsGame

Method:
minimumSwaps

Parameters:
vector <string>, string

Returns:
int

Method signature:
int minimumSwaps(vector <string> grid, string word)

(be sure your method is public)

Constraints

-
word will contain between 1 and 50 letters ('a'-'z', 'A'-'Z'), inclusive.

-
All characters in word will be distinct.

-
The number of elements in grid will be equal to the length of word.

-
The length of each element of grid will be equal to the length of word.

-
Each element of grid will contain only letters ('a'-'z', 'A'-'Z').

 

首先考慮到只有當(dāng)前行或者列包含所有word中的char 才可能通過調(diào)換得到word ,而且調(diào)換并不影響各個行列的內(nèi)容,因此首先排序判等。

問題轉(zhuǎn)化為word的一個permutation 最少通過幾次調(diào)換可以得到word

在permutation中,有cyclic representation的概念 :

例如 word = “abcd” sword = “adbc”即 要將 1423 轉(zhuǎn)換為 1234

那么1是一個cycle 423是一個cycle

要打破一個cycle 則需要元素- 1 次操作,我們要得到的即為所有cycle都為長度1

因此算法為:

#define REP(i,n) for(int (i)=0;i<n;i++)
 template<class T> void getMin(T& a,T b){if(b<a)a=b;}
template<class T> void getMax(T& a,T b){if(b>a)a=b;}  
int N ;
#define inf 99999999
string target;
int cacl2(string str){
    vector<int> vec(N);
    REP(i,N) vec[i] = target.find(str[i]);
    int ans=0;
    REP(i,N){
        int cnt=0; 
        int next = i;
        if(vec[next] != -1){
            while(vec[next] != -1){
                int tmp = next;
                next = vec[next];
                vec[tmp] = -1;
                cnt ++;
            }
            cnt --;
        }
        ans+=cnt;
    }
    return ans;
}
int cacl(string str){
    map<char,int> mp;
    REP(i,N)mp[str[i]] = i;
    bool flag = true;
    int ans = 0 ;
    while(true){
        flag = true;
        REP(i,N){
            if(str[i] != target[i]){
                str[mp[target[i]]] = str[i];
                mp[str[i]] = mp[target[i]];
                str[i] = target[i];
                flag = false;
                ans++;
            }
        }
        if(flag) break;
    }
    return ans;
}
class WordsGame
{
        public:
        int minimumSwaps(vector <string> grid, string word)
        {
                N = word.size();
                string sword = word;
                target = word;
                sort(sword.begin(),sword.end());
                int ans = inf;
                REP(i,N){
                    string str= grid[i];
                    string sstr = str;
                    sort(sstr.begin(),sstr.end());
                    if(sstr == sword)
                        getMin(ans,cacl2(str));
                }
                REP(i,N)
                    {
                    string str = "" ;
                    REP(j,N)str+= grid[j][i];
                    string sstr = str;
                    sort(sstr.begin(),sstr.end());
                    if(sstr == sword)
                         getMin(ans,cacl2(str));
                    }
                if(ans==inf)return -1;
                return ans;
        }
        

1000pt

Problem Statement

You have a piece of paper with exactly D positions laid out in a horizontal row. Each position looks like the following:

 _
|_|
|_|

There are 7 line segments in each position, and each line segment can hold exactly one match. Matches cannot be placed anywhere except on the line segments.
You are given an integer N containing exactly D digits (with no leading zeroes). Spell out the number using matches on the paper. Each digit must occupy a single position. The following diagram shows how each digit should be formed:
      _	               _        _                 _       _        _        _        _
 0 - | |  1 -  |  2 -  _|  3 -  _|  4 - |_|  5 - |_  6 - |_   7 -   |  8 - |_|  9 - |_|
     |_|      _|      |_        _|        |       _|     |_|        |      |_|       _|

After you lay out the initial arrangement, you are allowed to move up to K matches. You cannot discard matches or add new matches. After you make all your moves, the final arrangement must be valid (as described above) and the integer formed by the arrangement must contain the same number of digits as the original integer. Leading zeroes are allowed. Return the number of distinct integers that can be formed in this manner. Note that the original integer counts toward the total because it always obtainable by making 0 moves.
Definition

Class:
NumbersAndMatches

Method:
differentNumbers

Parameters:
long long, int

Returns:
long long

Method signature:
long long differentNumbers(long long N, int K)

(be sure your method is public)

Constraints

-
N will be between 1 and 10^18 - 1, inclusive.

-
K will be between 1 and 126, inclusive.

Examples

0)

10
1
Returns: 4

Here you can compose numbers 10, 19, 16 and 70:

      _                     _
  |  | |     ----->     |  | |
 _|  |_|               _|  |_|
      _                     _
  |  | |     ----->     |  |_|
 _|  |_|               _|   _|
      _                     _
  |  | |     ----->     |  |_ 
 _|  |_|               _|  |_|
      _                _    _
  |  | |     ----->     |  | |
 _|  |_|                |  |_|

1)

23
1
Returns: 4

This time it's possible to compose 22, 23, 25 and 33.

2)

66
2
Returns: 15

Here you can move up to 2 matches, so quite a lot of numbers can be composed. Note that you are allowed to move a match from one digit to another one, so, for example, it's possible to compose 38. However, you can't discard a match or add a new match, so, for example, you can't compose 55 or 88.

3)

888888888
100
Returns: 1

You are allowed to move a lot of matches, but still it's only possible to compose 888888888.

4)

444444444444444444
2
Returns: 1

Given that at most 2 matches can be moved, only the initial number can be composed.

 

7段碼的題,首先用數(shù)組記錄10個數(shù)字的七段碼,這樣比較兩個數(shù)字需要移動的火柴個數(shù)只需要比較七個位子上兩者的關(guān)系即可。而后考慮執(zhí)行到第k個數(shù)字時候,已經(jīng)添加了inc個火柴,減去了dec個火柴,那么要求這個數(shù)字的改變能得到的數(shù)字?jǐn)?shù)目只需要枚舉變成10個數(shù)字即可,最終如果inc和dec相等則滿足題意。因此用dp備忘錄或者使用遞推來求解,顯然遞推的效率更高但不容易想到下面是代碼

回朔+memo

Code Snippet
typedef long long int64;  
typedef vector<int> VI;
typedef vector<string> VS;
#define REP(i, n) for (int i = 0; i < (n); ++i)
template<class T> inline void checkmin(T &a,const T &b) { if (b<a) a=b; }
template<class T> inline void checkmax(T &a,const T &b) { if (b>a) a=b; }

int dis[10][7]={
    {1,1,1,0,1,1,1},{0,0,1,0,0,1,1},
    {1,0,1,1,1,0,1},{1,0,1,1,0,1,1},
    {0,1,1,1,0,1,0},{1,1,0,1,0,1,1},
    {1,1,0,1,1,1,1},{1,0,1,0,0,1,0},
    {1,1,1,1,1,1,1},{1,1,1,1,0,1,1}
};
int K,N,A[30];
int64 memo[30][128][128];
int64 solve(int depth,int inc,int dec){
    if(depth == N)return inc==dec?1:0;
    if(memo[depth][inc][dec] != -1)return memo[depth][inc][dec];
    memo[depth][inc][dec]=0;
    int64& ret=memo[depth][inc][dec];
    REP(i,10){
        int more=0,less=0;
        REP(j,7){
            if(dis[i][j] == 1 && dis[A[depth]][j] == 0)
                more++;
            if(dis[i][j] == 0 && dis[A[depth]][j] == 1)
                less++;
        }
        if(more+inc>K||dec+less>K) continue;
        ret += solve(depth+1,inc+more,less+dec);
    }
    return ret;
}
class NumbersAndMatches
{
        public:
        long long differentNumbers(long long _N, int _K)
        {
               K=_K;N=0;
               memset(memo,-1,sizeof(memo));
               while(_N>0){
                    A[N++]=_N%10;_N/=10;
               }
               return solve(0,0,0);
        }

 

遞推的:

typedef long long int64;  
typedef vector<int> VI;
typedef vector<string> VS;
 
#define REP(i, n) for (int i = 0; i < (n); ++i) 
 
#define two(X) (1<<(X))
#define contain(S,X) ((S&two(X))>0)
int dis[10][7]={
    {1,1,1,0,1,1,1},{0,0,1,0,0,1,1},
    {1,0,1,1,1,0,1},{1,0,1,1,0,1,1},
    {0,1,1,1,0,1,0},{1,1,0,1,0,1,1},
    {1,1,0,1,1,1,1},{1,0,1,0,0,1,0},
    {1,1,1,1,1,1,1},{1,1,1,1,0,1,1}
};
int64 dp[128][128];
class NumbersAndMatches
{
        public:
        long long differentNumbers(long long N, int K)
        {
               REP(i,K+1)REP(j,K+1) dp[i][j] = !i && !j;
               while(N>0){
                    int now = N%10;N/=10;
                    for(int i=K;i>=0;i—)//注意計算順序要從大向小,因為從小向大會更新大的數(shù)組值~
                        for(int j=K;j>=0;j--){
                            if(dp[i][j]){
                                int64 x = dp[i][j];
                                dp[i][j] = 0;
                                REP(y,10){
                                    int inc=0,dec=0;
                                    REP(z,7)    {
                                        if(dis[y][z]==0 && dis[now][z]==1)
                                            dec++;
                                        if(dis[y][z]==1 && dis[now][z]==0)
                                            inc++;
                                    }
                                    if(inc+i>K||dec+j>K)continue;
                                    dp[i+inc][j+dec] += x;
                                }
                            }
                    }
                   }
               int64 ret=0;
               REP(i,K+1) ret+=dp[i][i];
               return ret;
        }

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


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美色视频日本高清在线观看| 午夜精品视频在线观看| 欧美3dxxxxhd| 能在线观看的日韩av| 久久中文欧美| 欧美国产精品va在线观看| 欧美ab在线视频| 欧美日韩国产经典色站一区二区三区| 欧美精品在线视频观看| 欧美日韩综合一区| 国产精品一卡二| 亚洲第一伊人| 亚洲午夜久久久久久久久电影网| 欧美在线亚洲一区| 欧美aa国产视频| 亚洲国产婷婷香蕉久久久久久| 亚洲国产视频一区二区| 亚洲欧美日韩一区二区| 国产精品综合不卡av| 亚洲国内欧美| 国产午夜久久久久| 中文av字幕一区| 欧美视频在线观看免费| 国产精品视频专区| 一区二区三区精品久久久| 狠狠色狠狠色综合日日91app| 欧美在线亚洲| 欧美一区二区三区婷婷月色| 久久人人精品| 欧美日韩中文在线观看| 国产一区二区三区四区五区美女 | 欧美午夜精品久久久久久超碰| 国产欧美日韩三区| 亚洲精品女人| 欧美一乱一性一交一视频| 欧美二区视频| 国产亚洲高清视频| 亚洲调教视频在线观看| 欧美激情视频在线播放| 尤物精品国产第一福利三区 | 一区二区三区无毛| 亚洲小说春色综合另类电影| 欧美电影免费| 久热精品视频| 国产午夜精品久久久久久久| 亚洲一区二区三区激情| 亚洲破处大片| 久久综合久久综合久久| 国产欧美日韩精品丝袜高跟鞋| 99国产精品国产精品久久| 免费欧美在线视频| 久久激情久久| 激情久久久久久久| 久久亚洲欧美国产精品乐播| 亚洲你懂的在线视频| 国产精品久久久久高潮| 亚洲天堂免费观看| 一区二区三区四区国产| 欧美激情亚洲一区| 久久久蜜桃精品| 韩日精品视频一区| 久久精品视频免费观看| 亚洲欧美中文在线视频| 国产视频观看一区| 欧美专区18| 久久高清一区| 一区二区欧美激情| 欧美日韩精品一区视频| 中文精品视频一区二区在线观看| 亚洲国产岛国毛片在线| 久久在线观看视频| 久久夜色精品亚洲噜噜国产mv| 在线观看视频一区二区| 欧美福利视频一区| 欧美乱大交xxxxx| 亚洲一区二区高清| 午夜精品久久久久久久男人的天堂| 国产日韩欧美另类| 免费日韩视频| 欧美日本国产一区| 午夜精品免费视频| 久久久久国内| 亚洲伦理在线观看| 亚洲永久免费| 亚洲国内精品在线| 一区二区三区久久精品| 国产精品一区二区久久| 久热爱精品视频线路一| 欧美巨乳在线观看| 久久精品国产免费看久久精品| 久久视频在线免费观看| 一道本一区二区| 欧美一级专区免费大片| 日韩视频在线观看免费| 亚洲女爱视频在线| 亚洲精品一二三| 香蕉久久夜色精品国产| 亚洲精品日本| 午夜精品久久久久久久99樱桃 | 午夜精品久久久久久久白皮肤| 欧美在线免费一级片| aⅴ色国产欧美| 久久精品国产综合| aa成人免费视频| 久久久久久久久久久成人| 一区二区av在线| 久久免费国产精品1| 午夜欧美视频| 欧美**字幕| 久久久久一本一区二区青青蜜月| 欧美风情在线观看| 久久久久久夜精品精品免费| 欧美日韩国产小视频| 欧美成人福利视频| 国产欧美在线观看| 宅男噜噜噜66国产日韩在线观看| 在线精品福利| 香蕉亚洲视频| 一本色道久久综合狠狠躁的推荐| 免费一级欧美片在线播放| 欧美视频中文一区二区三区在线观看| 久久精品国产亚洲aⅴ| 欧美午夜剧场| 91久久久亚洲精品| 亚洲国产成人porn| 99re国产精品| 老鸭窝亚洲一区二区三区| 欧美专区日韩专区| 国产精品一区二区久久国产| 亚洲久久一区| 一区二区电影免费观看| 欧美a级一区| 欧美国产精品一区| 亚洲国产黄色| 蜜桃av综合| 亚洲高清自拍| 一个人看的www久久| 欧美高清影院| 亚洲精品黄色| 亚洲影视综合| 国产精品久久久久久久久免费樱桃| 日韩天堂在线观看| 亚洲自啪免费| 国产欧美日韩在线观看| 午夜精品理论片| 久久久久久9| 尤物yw午夜国产精品视频| 美玉足脚交一区二区三区图片| 欧美激情国产日韩| 一本一本久久| 国产精品久久久久77777| 午夜国产精品影院在线观看 | 欧美国产国产综合| 亚洲欧洲一区二区天堂久久| 欧美精品系列| 亚洲综合视频一区| 久久大香伊蕉在人线观看热2| 禁断一区二区三区在线| 免费亚洲电影在线| 日韩网站在线| 久久久久看片| 亚洲精品久久7777| 国产精品美女午夜av| 先锋影音久久久| 蜜臀va亚洲va欧美va天堂| 99视频精品| 国产欧美一区二区精品性色| 久久久亚洲高清| 日韩视频一区| 久久中文久久字幕| 日韩一本二本av| 国产亚洲免费的视频看| 欧美91福利在线观看| 在线综合+亚洲+欧美中文字幕| 鲁大师影院一区二区三区| 日韩视频一区二区| 国产一区二区三区高清在线观看| 男女精品视频| 亚洲自拍偷拍福利| 美女精品自拍一二三四| 亚洲欧美卡通另类91av| 在线成人av.com| 国产精品家庭影院| 欧美成人精品在线播放| 亚洲资源av| 99re在线精品| 欧美激情国产高清| 一区二区三区在线免费播放| 欧美破处大片在线视频| 久久国产精品72免费观看| 亚洲女人天堂成人av在线| 亚洲国产精品成人综合色在线婷婷 | 国产亚洲精品成人av久久ww| 欧美精品一卡二卡| 久久国内精品自在自线400部| 亚洲日本欧美日韩高观看| 蜜桃av一区二区三区| 久久aⅴ国产欧美74aaa| 亚洲午夜av在线| 最近看过的日韩成人|