zoukankan      html  css  js  c++  java
  • ural 1007. Code Words

    1007. Code Words

    Time Limit: 2.0 second
    Memory Limit: 64 MB
    A transmitter sends over a noisy line some binary code words. The receiver on the other end uses special technique to recover the original words.
    Every word originally consists of symbols 0 and 1. All words have the same length N (4 ≤ N ≤ 1000). After traveling through the noisy line one (but no more) of the following modifications to a word may occur:
    1. Any (but only one) symbol 0 is replaced by 1.
    2. Any (but only one) symbol is removed.
    3. A symbol (0 or 1) is inserted at any position.
    It is known that the original words all have the following property: the sum of positions where symbols 1 are located is a multiple of (N+1) or equal to zero.

    Input

    Input contains number N followed by received words. The words are delimited with line breaks. There will be no more than 2001 words. There is nothing else in the input data, except maybe for some extra spaces or line breaks.

    Output

    Your program should print to output the original sequence of words as they were transmitted. The words should be delimited by line breaks.

    Sample

    inputoutput
    4    
    0000 
    011  
    1011 
    11011
    
    0000
    0110
    1001
    1111
    
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    int n;
    bool isSourceStr(string src){
        int cnt = 0;
        for(int i = 0; i < src.length(); i ++ )
            if(src[i] == '1') cnt +=(i+1);
        if(cnt%(n+1)) return false;
        else return true;
    }
    
    int main(){
        cin >> n;
        string receive;
        while(cin>>receive) {
            if(receive.length() == n){
                int cnt = 0;
                for(int j = 0; j < receive.length(); j ++ ){
                    if(receive[j] == '1') cnt +=(j+1);
                }
                if(cnt%(n+1) != 0){
                    int a = cnt%(n+1);
                    while(receive[a-1]!='1'){
                        a +=(n+1);
                    }
                    receive[a-1]='0';
                }
                cout<<receive<<endl;
            }
            else if(receive.length() < n){
                for(int i = 0; i <= receive.length(); i ++ ){
                    string tmp1=receive,tmp2=receive;
                    tmp1.insert(i,1,'1');
                    if(isSourceStr(tmp1)){cout<<tmp1<<endl;break;}
                    tmp2.insert(i,1,'0');
                    if(isSourceStr(tmp2)){cout<<tmp2<<endl;break;}
                }
            }
            else{
                for(int i = 0; i <= receive.length(); i ++ ){
                    string tmp1=receive;
                    tmp1.erase(i,1);
                    if(isSourceStr(tmp1)){cout<<tmp1<<endl;break;}
                }
            }
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    idea报错。Error:Failed to load project configuration: cannot parse xml file E:project.ideaworkspace.xml: Error on line 1: 前言中不允许有内容。
    数据库索引的使用
    sql查询每个学生的最高成绩mysql语句
    Tomcat下没有编译后的class文件
    fullpage中大的图片超过一屏怎么在手机端滑动显示?
    springmvc拦截器的配置、使用
    过滤器、拦截器中重定向无限循环问题解决
    Linux下的Memcache安装
    Windows下的Memcache安装
    jQuery学习资源参考教程网址推荐
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3043181.html
Copyright © 2011-2022 走看看