"Programming language integrated design" on the second week of exercise machine

5 longest common substring

Given two strings a, b, k prior chance in a string of characters to be modified, so that the longest common substring modified two longest string. Each modification can choose a, an arbitrary position in a string b modify an arbitrary character string.

Input Format

The first line includes a positive integer k.
The second and third rows, respectively of the input string a, b. (The length of each string does not exceed 500)

Output Format

Output is an integer representing the length of the longest common substring of two strings modified.

SAMPLE INPUT

5
aaaaa
bbbbb

Sample Output

5

Accepted

Get two longest common substring of a string, the substring required to be contiguous in the original string. And the longest common subsequence does not require continuous.



#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
using namespace std;
int calculate(string a,string b,int k){ //计算字符串开头开始最长的公共子串
    int pos=0; //已访问位置
    int K=0;//差异
    while(pos<a.size()&&pos<b.size()&&(a[pos]==b[pos]||K<k)){
        if(a[pos]!=b[pos]) K++;
        pos++;
    }
    return pos;
}
        int main(){
            int k;
            int lena,lenb;
            int max=0;
            int i,j;
            int ans=0;
            string a,b;
            cin >>k;
            cin >> a >> b;
            lena=(int)a.size();
            lenb=(int)b.size();
            for(i=0;i<lena;i++){
                for(j=0;j<lenb;j++){
                    max=calculate(a.substr(i),b.substr(j),k); //substr :主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度。
                    if(ans<max) ans=max;
                }
            }
            cout << ans << endl;
            return 0;
        }

6 rotating dice

Martha has n dice, each dice surface 6 are exactly a number between 0 and 9, and will not repeat the same number of a die face 6.
Now Martha will use the n to create a new digital screens. She n dice are placed in a row, then from left to right to view and read the upper surface of the die, to obtain a new number. Then she continued rotation surface of each dice can get new and different numbers. Rotating dice need to meet the following rules: 1, the number produced can not include leading zeros; 2, need not use all the dice when creating new number; 3, using the rotary die, can not be converted into digital numbers 9 6, and vice versa.
Given n dice, Martha can use them constitute all integers from 1 to x. Martha wants to know, for a given n dice, the maximum value of x is how much?

Input Format

Only a first line integer n, the number (1≤n≤3) dice.
Next n lines, each line 6 comprises integers a [i] [j] ( 0≤a [i] [j] ≤9), the j-th surface number represents i-th dice.

Output Format

Output an integer, i.e. the maximum number of x, Martha can use her dice configuration numbers from 1 to x. If you can not constitute 1, the output is 0.

SAMPLE INPUT

3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9

Sample Output

98

Accepted

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
using namespace std;
        int main(){
            int n;
            int i,j,k,t;
            int a[500][500];
            cin >> n;
            for(i=1;i<=n;i++){
                for(j=1;j<=6;j++)
                    cin >> a[i][j];
            }
           //n==1
            if(n==1){
                for(j=1,k=1;j<=6;j++){
                    if(a[1][j]==k) k++;
                }
                k--;
                cout << k << endl;
                return 0;
            }
            //n==2
           if(n==2){
               for(j=1,k=1;j<=6;j++){
                   for(t=1;t<=6;t++)
                   if(a[1][t]==k||a[2][t]==k) k++;
               }
               k--;
               if(k<9){cout << k << endl;return 0;}
               else{
                   for(k=10,j=1;j<=35;j++){
                       for(i=1;i<=6;i++){
                           for(t=1;t<=6;t++)
                           if(a[1][i]*10+a[2][t]==k||a[1][i]+a[2][t]*10==k) k++;
                       }
                   }
                   k--;
                   cout << k << endl;return 0;
               }
           }
            
            if(n==3){
                for(j=1,k=1;j<=6;j++){
                    for(t=1;t<=6;t++)
                    if(a[1][t]==k||a[2][t]==k||a[3][t]==k) k++;
                }
                k--;
                if(k<9){cout << k << endl;return 0;}
                else{
                    for(k=10,j=1;j<=35;j++){
                        for(i=1;i<=6;i++){
                            for(t=1;t<=6;t++) if(a[1][i]*10+a[2][t]==k||a[1][t]+a[2][i]*10==k||a[1][i]*10+a[3][t]==k||a[1][t]+a[3][i]*10==k||a[2][i]*10+a[3][t]==k||a[2][t]+a[3][i]*10==k) k++;
                    
                       }
                    }
                    k--;
                    cout << k << endl;return 0;
                }
            }
            return 0;
        }

7 Equal pen

n individual circle, each have ai pens. Each person can pass the pen to the neighboring people around, every time the energy consumed by passing a pen per person 1. Seeking to give all individuals equal number of pens minimum energy.

Input Format

A first line integer n, the number (30% of the data, n <= 1000; 100% data, n <= 1e6) people.
Next n lines, each line an integer ai.

Output Format

Output An integer representing the minimum energy equal access for all to the pen. (The answer can be used to ensure that 64-bit signed integer store)

SAMPLE INPUT

4
1
2
5
4

Sample Output

4

Accepted

Reference Source: Candy pass
for each person, assuming he's items to go through two types of changes: 1, take from a person. 2, give a person before the final result is a change becomes aver.

  • a[1]-X1+X2=aver X2=X1-(a[1]-aver)
  • a [2] -X2 + X3 = aver X3 = X1- (a [2] + a [1] -2 * aver)
    provided c [1] = a [1 ] -aver c [2] = c [1] + a [2] -aver
    there c [i] = c [i -1] + a [i] -aver
    there are problems | X1 | + | X2 | + ...... + | Xn | , and each minimum can be expressed by X1 | X1 | + | X1- c [1] | + | X1-c [2] | + ...... + | X1-c [n-1] |
    absolute meaning but also represents an Xi axis distance to Ci, so the question becomes: given n points on the number line, to find a distance and try their small point, but the point is that these numbers median,
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
using namespace std;
int a[1000005]={0},x[1000005]={0};
        int main(){
            int n;
            long long sum=0,num=0;
            int i=0,j;
            int aver;
            cin >> n;
            for(i=0;i<n;i++){
                cin >> a[i];
                sum+=a[i];
            }
            aver=(int)(sum/n);
            for(i=0;i<n;i++){
                x[i]=x[i-1]-a[i-1]+aver;
            }
            sort(x,x+n);
            j=n/2;
            for(i=0;i<n;i++){
                num+=abs(x[j]-x[i]);
            }
            cout << num << endl;
            return 0;
        }

Appendix: mathematical proof

On the number line there are n points, find a point x, the distance and the minimum so that she each point. Prove: this number is the median number of n represented by that point. If we take two points on a number line are paired, the largest minimum feature, the second largest small ...... times with the point to the nearest point from each of the two intermediate points, then

if there are an odd number of points, then obviously the middle of the point it is asked for.
∴ number indicates the point which is the median number n is proved.

Guess you like

Origin www.cnblogs.com/lvhang/p/12386018.html