天天看点

Codeforces230C--Shifts--想法题--类似palindrome

C. Shifts time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.

To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".

Determine the minimum number of moves needed to make some table column consist only of numbers 1.

Input

The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.

It is guaranteed that the description of the table contains no other characters besides "0" and "1".

Output

Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.

Sample test(s) input

3 6
101010
000100
100000
      

output

3
      

input

2 3
111
000
      

output

-1
      

Note

In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.

In the second sample one can't shift the rows to get a column containing only 1s.

一道想法题,

题意so简单,就把一个矩阵中某一行向左移或者向右移使得其中有一列全为1。

问了学长才想懂。

一种把原串对称的思想!!!

因为串是循环的,即比如串

001010 我们可以将其扩展为 001010 001010.      

从下标为1开始从左往右扫到底,再从下标2*m从右往左扫一遍扫到底。

即在O(n*m)时间内内预处理出 每个点离它最近的那个 1的位置。

从左往右扫,考虑后面那个串。(因为要统计串中最后一个1的位置)

从右往左扫,考虑前面那个串。(因为要统计串中最后一个1的位置)

类似的思想还应用在回文串中,即将一个串复制展开成两个串,然后去看回文,平时多做题,注意积累!

一下是这道题的代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

char du[110][10100];
int matrix[110][10100];
int double_matrix[110][20200];
int ans[110][10100];

int main()
{
    //freopen("input.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);
    bool flag1=true;
    for(int i=1;i<=n;++i)
    {
        scanf("%s",du[i]);
        bool flag2=false;
        for(int j=1;j<=m;++j)
        {
            matrix[i][j]=du[i][j-1]-'0';
            if(!flag2&&matrix[i][j]) flag2=true;
            double_matrix[i][j]=double_matrix[i][j+m]=matrix[i][j];
        }
        if(!flag2) flag1=false;
    }
    if(!flag1) printf("-1\n");
    else
    {
        for(int i=1;i<=n;++i)
        {
            int last;
            for(int j=1;j<=m;++j)
            {
                if(double_matrix[i][j])
                    last=j;
            }
            for(int j=m+1;j<=2*m;++j)
            {
                if(double_matrix[i][j])
                {
                    last=j;
                }
                else
                {
                    ans[i][j-m]=j-last;
                }
            }
            for(int j=2*m;j>=m+1;--j)
            {
                if(double_matrix[i][j])
                    last=j;
            }
            for(int j=m;j>=1;--j)
            {
                if(double_matrix[i][j])
                {
                    last=j;
                    ans[i][j]=ans[i-1][j];
                }
                else
                {
                    ans[i][j]=min(ans[i][j],last-j)+ans[i-1][j];
                }
            }
        }
        int hehe=ans[n][1];
        for(int i=2;i<=m;++i)
            hehe=min(hehe,ans[n][i]);
        printf("%d\n",hehe);
    }
    return 0;
}