LoginSignup
0
0

More than 5 years have passed since last update.

NxMの行列で要素が0であればその行と列のすべてを0にするプログラム

Last updated at Posted at 2019-01-02

そーすこーど

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
  int N,M;
  cin>>N>>M;
  vector<string> matrix(N);
  int i,j;
  for(i=0;i<N;i++){
    cin>>matrix[i];
  }
  int row_mask=0;
  int col_mask=0;
  for(i=0;i<N;i++){
    for(j=0;j<M;j++){
      if(matrix[i][j]=='0'){
        row_mask|=(1<<i);
        col_mask|=(1<<j);
      }
    }
  }
  for(i=0;i<N;i++){
    for(j=0;j<M;j++){
      if(row_mask&(1<<i)||col_mask&(1<<j)){
        cout<<0;
      } else{
        cout<<matrix[i][j];
      }
    }
    cout<<endl;
  }
  return 0;
}

入力例1

3 3
111
101
111

出力例1

101
000
101

入力例2

2 33
111111111111111111111111111111110
111111111111111111111111111111111

出力例2

000000000000000000000000000000000
011111111111111111111111111111110
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0