18_01_14-22_零碎题目总结

02_A_Long long words

如果一个字符串可以被某个长度为k的字符串重复多次得到,则称这个字符串的周期为k。例如,字符串“abcabcabcabc”以3为周期(当然,他也以6、12等等为周期)。 现在请你编写一个程序,求出任一长度不超过80的字符串的最小周期。

Input

输入首先是一个整数n,代表有n组数据。每组数据占一行,是一个长度不超过80的字符串。两组相邻的输入之间有一个空行。

Output

每组数据在一行内输出一个整数k,代表该字符串的最小周期。两组相邻的输出之间应当有一个空行。

AC代码:

#include <iostream>
#include <string.h>

using namespace std;

int main(){
    int n,l,i,j,t;
    char s[80];
    
    cin>>n;
    for(i=0;i<n;i++){
        cin>>s;
        l=strlen(s);
        for(j=1;j<=l;j++){
            t=1;
            for(int k=0;j+k<l;k++){
                if(s[j+k]!=s[k]){
                    t=0;
                    break;
                }
            }
            if(t==1&&l%j==0){
                cout<<j<<endl;
                break;
            } 
            if(j==l){
                cout<<l<<endl;
                break;
            }    
        } 
        
        if(i!=n-1) cout<<endl;
    }
} 

02_B_Decimal and quasibinary

将输入数字转化为由0和1组成的多个数字的和,排序输出这些数字和这些数字的个数。

AC_样例:

#include <bits/stdc++.h>
using namespace std;

char num[10];
vector<int> ans;
int main(){
    scanf("%s",num);
    int len = strlen(num);
    bool ok=1;
    while(ok){
        int tmp=0;
        ok=0;
        for(int i=0;i<len;i++){
            if(num[i]>'0')tmp=tmp*10+1,ok=1,num[i]--;
            else tmp*=10;
        }
        if(tmp) ans.push_back(tmp);
    }
    int sz=ans.size();
    printf("%d\n",sz);
    for(int i=0;i<sz;i++)
        printf("%d%c",ans[i],i==sz-1?'\n':' ');
    
    return 0;
} 

02_E_High-speed search

A、B、C三个数组,判断是否存在三个数分别属于三个数组相加等于X

思路:对A、B求和并排序,然后判断是否和X-Ci相等。

AC_代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std; 

int main(){
    int l,n,m,s;
    int a[500],b[500],c[500],x;
    int sum[250000],suml,i,j,d,pd,ca=1;
    
    while(cin>>l>>n>>m){
    for(i=0;i<l;i++) cin>>a[i];
    for(i=0;i<n;i++) cin>>b[i];
    for(i=0;i<m;i++) cin>>c[i];
    memset(sum,0,250000);
    int t=0;
    for(i=0;i<l;i++){
        for(j=0;j<n;j++){
            sum[t]=a[i]+b[j];
            ++t;
        }
    }
    suml=t;
    sort(sum,sum+suml);           // cout<<suml<<endl; 
    cout<<"Case "<<ca<<":"<<endl;
    ++ca;
    
    cin>>s;
    int jl,jr;    
    while(s--){
        cin>>x;
        pd=0;
        for(i=0;i<m;i++){
            d=x-c[i];         
            jl=1;jr=suml;
            for(j=(jl+jr)/2;jr>jl&&jr-jl>1;){                    
                    //    cout<<j<<" "<<jr<<" "<<jl<<" "<<" "<<d<<" "<<sum[j]<<endl;
                if(sum[j]==d)  { pd=1;break;}
                else if(sum[j]>d) {jr=j;j=(jl+j)/2; }
                else {jl=j;j=(j+jr)/2; }
                //cout<<"1";
            }               // cout<<"2 "<<k<<" "<<pd[k]<<endl;
            if(pd==1) break;      
        }               //     cout<<" "<<k<<" "<<pd[k]<<endl;
        if(pd==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    } 
    }
    return 0;    
}

 CF_911B_Two Cakes

It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

  1. Each piece of each cake is put on some plate;
  2. Each plate contains at least one piece of cake;
  3. No plate contains pieces of both cakes.

To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input

The first line contains three integers na and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output

Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

AC代码:

#include <iostream>

using namespace std;

int main(){
    int n,a,b,i;
    int at,bt,mint,maxx;
    
    cin>>n>>a>>b;
    maxx=0;
    for(i=1;i<n;i++){
        at=a/i;
        bt=b/(n-i);
        if(at>bt)mint=bt;
        else mint=at;
        if(mint>maxx)maxx=mint;
    }
    cout<<maxx<<endl;
} 

 CF_116B_Little Pigs and Wolves

Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.

A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.

They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.

What is the maximum number of little pigs that may be eaten by the wolves?

Input

The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.

It is guaranteed that there will be at most one wolf adjacent to any little pig.

Output

Print a single number — the maximal number of little pigs that may be eaten by the wolves.

CFAC_vjWA代码:

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

int main(){
    int n,m,ans;
    char cell[11][11];
    int i,j,k;
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    
    cin>>n>>m;
    ans=0;
    for(i=0;i<n;i++)
        cin>>cell[i];
    for(i=0;i<n;i++)
        for(j=0;j<m;j++){  // cout<<"1"<<endl;
            if(cell[i][j]=='W'){    // cout<<"2"<<endl;
                for(k=0;k<4;k++){         //  cout<<"3"<<endl;
                    if(cell[i+dx[k]][j+dy[k]]=='P'){  // cout<<"4"<<endl;
                        ++ans;
                    //    cout<<ans<<endl;
                        break;
                    }
                }
            }
        }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2018-01-22 14:36  Complex_Analysis  阅读(194)  评论(0编辑  收藏  举报