Chapter 4 Learning Summary

Keywords: C++ network

Chapter 4 is about strings, arrays and generalized tables. In the process of doing the problem, strings are gradually found to be powerful in STL (the next goal is to be familiar with the functions of strings).

Skyscraper L1-8 AI Core Code Valuated at 100 million

Finally, the problem has been solved. The idea is to divide the levels as far as possible. The idea should be clear and rigorous. It is absolutely no problem to write more judgement functions.

AC Code: (Origin)

#include <iostream>
#include <string.h>
using namespace std;
char map[1005];
char a[1005],b[1005],c[1005];
int alen=0,blen=0,clen=0;
bool vis[1005];
bool isbiao(char a)
{
    if(a>='a'&&a<='z'||a>='A'&&a<='Z'||a>='0'&&a<='9'||a==' ') return false ;
    else return true;
}
bool isspace(char a)
{
    return a==' ';
}
bool isalone(char a)
{
    return isbiao(a)||isspace(a)||a=='\0';
}
bool iscan(char a[])
{
    return !strcmp(a,"can you");
}
bool iscould(char a[])
{
    return !strcmp(a,"could you");
}
void solve_kong_big_wen()
{
    alen=0;
    int len=strlen(map),i=0;
    if(map[i]==' '){//Processing Preemptive Spaces 
        while(map[i]==' '){
            ++i;
        }
    }
    for(;i<len;++i){
        if(map[i]==' '){
            if(i+1<len && !isspace(map[i+1]) && !isbiao(map[i+1])) a[alen++]=map[i];
        }
        else if(map[i]=='?') a[alen++]='!';
        else if(map[i]>='A'&&map[i]<='Z'&&map[i]!='I') a[alen++]=char(map[i]+32);
        else a[alen++]=map[i];
    }
    a[alen]='\0';
}
void solve_can()
{
    memset(vis,0,sizeof(vis));
    blen=0;
    for(int i=0;i<alen;++i){
        if(a[i]=='c'){
            int flag=0;
            if(i+6<alen) {
                if(!((i-1>=0&&isalone(a[i-1])||i-1==-1)&&(i+7<=alen&&isalone(a[i+7])))) ;    
                else {
                    char m[10];
                    strncpy(m,a+i,7);
                    m[7]='\0';
                    if(iscan(m)) {
                        vis[blen]=1;b[blen++]='I';b[blen++]=' ';b[blen++]='c';b[blen++]='a';b[blen++]='n';i+=6;
                        continue;
                    }
                }
                
            }        
            if(i+8<alen){
                if(!((i-1>=0&&isalone(a[i-1])||i-1==-1)&&(i+9<=alen&&isalone(a[i+9])))) {
                    b[blen++]=a[i];continue;
                }
                char m[15];
                strncpy(m,a+i,9);
                m[9]='\0';
                if(iscould(m)) {
                    vis[blen]=1;b[blen++]='I';b[blen++]=' ';b[blen++]='c';b[blen++]='o';b[blen++]='u';b[blen++]='l';b[blen++]='d';i+=8;
                }
                else b[blen++]=a[i];
            }
            else b[blen++]=a[i];
        }
        else b[blen++]=a[i];
    }
    b[blen]='\0';
}
void solve_I()
{
    clen=0;
    for(int i=0;i<blen;++i){
        if(b[i]=='I'&&!vis[i]){
            if(!((i-1>=0&&isalone(b[i-1])||i-1==-1)&&(i+1<=blen&&isalone(b[i+1])))) c[clen++]=b[i];
            else {
                c[clen++]='y';c[clen++]='o';c[clen++]='u';
            }
        }
        else if(b[i]=='m'&&i+1<blen&&b[i+1]=='e'){
            if(!((i-1>=0&&isalone(b[i-1])||i-1==-1)&&(i+2<=blen&&isalone(b[i+2])))) c[clen++]=b[i];
            else {
                c[clen++]='y';c[clen++]='o';c[clen++]='u';i++;
            }
        }
        else c[clen++]=b[i];    
    }
    c[clen]='\0';
}
int main()
{
    int N;
    cin>>N;
    getchar();
    while(N--){
        cin.getline(map,1004);
        solve_kong_big_wen();
        solve_can();
        solve_I();
        cout<<map<<endl;
        cout<<"AI: "<<c<<endl;
    }
    return 0;
}

The topic I want to discuss is 7-1 sparse matrices.

This problem has not been deleted and increased, only queries, it feels like opening a structured array to store data + A for loop traversal can be solved, but I'll try to think about the cross linked list.

(Photo Source Network)

The figure above is just like a sparse matrix. It has a head pointer for each row, a head pointer for each column, and n nodes.

Graph of nodes:

(Photo Source Network)

A node represents information of a non-zero number in a matrix

Includes row s, col umns, value s, right pointers to the right, down pointers to the following

Here are my steps to create a cross-linked list:

First: Definition (Node + Link List)

int n,m,t; // Information to be input, step 2

Define nodes:

typedef struct node{
int x,y,w; //x represents rows, Y represents columns, and W represents values
    node *down,*right;
}node;
Define a linked list:
struct crosslist{
node *rhead[505],*chead[505]; // pointer per row, pointer per column
int hang,lie,num; // highlight my poor English
}M;
 
2: Input the number of rows, columns and non-zero elements of sparse matrix and initialize the pointer
cin>>n>>m>>t;
For (int I = 1; I < = n + 5; ++ i) {// Habits Open a little more space
        M.rhead[i]=NULL;
    }
for(int i=1;i<=m+5;++i){
        M.chead[i]=NULL;
   }
 
Three: Input information for each point
for(int k=1;k<=t;++k){
// Input row, column, value
        cin>>i>>j>>w;
// Open a node and assign values
        p=new node;
        p->x =i;
        p->y =j;
        p->w =w;
// Processing the row to determine if it has elements or if the first element of the row is on the right of the element to be inserted, insert it directly
        if(M.rhead[i]==NULL || M.rhead[i]->y>j ){
            p->right =M.rhead[i];
            M.rhead[i]=p;
        }
// Otherwise, locate where you should insert and insert
        else {
For (q = M. Rhead [i]; Q - > right!= NULL & & Q - > right - > y < j; q = q - > right); // Note that this semicolon, for loop is just to move the pointer to the right place, without processing the elements.
                p->right=q->right;
                q->right=p;
        }
// The treatment of this column is the same as that of the line.
        if(M.chead[j]==NULL || M.chead[j]->x >i ){
            p->down=M.chead[j];
            M.chead[j]=p;
        }
        else {
            for(q=M.chead[j];q->down!=NULL&&q->down->x<i ;q=q->down );
            p->down=q->down;
            q->down=p;
        }
    }
}
 
In this way, the cross-linked list is built, and a query function can be written to solve the problem (simple problems complicated)
Note: This code is just for problem solving. It is highly targeted and has many loopholes. Written mainly to share ideas.
Attach AC code:
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cmath>
using namespace std;
#define maxn 1000010
int n,m,t;

typedef struct node{
    int x,y,w;
    node *down,*right;
}node;
struct crosslist{
    node *rhead[505],*chead[505];//Row, column 
    int hang,lie,num;
}M;

void create()
{
    node *p,*q;
    cin>>n>>m>>t;
    M.hang=n;
    M.lie=m;
    M.num=t;  
    for(int i=1;i<=n+5;++i){
        M.rhead[i]=NULL; 
    }
    for(int i=1;i<=m+5;++i){
        M.chead[i]=NULL; 
    }
    int i,j,w;
    for(int k=1;k<=t;++k){
        cin>>i>>j>>w;
        p=new node;
        p->x =i;
        p->y =j;
        p->w =w;
        if(M.rhead[i]==NULL || M.rhead[i]->y>j ){
            p->right =M.rhead[i];
            M.rhead[i]=p;
        }
        else {
            for(q=M.rhead[i];q->right!=NULL&&q->right->y < j;q=q->right);
                p->right=q->right; 
                q->right=p;
        }
        
        if(M.chead[j]==NULL || M.chead[j]->x >i ){
            p->down=M.chead[j]; 
            M.chead[j]=p; 
        }
        else {
            for(q=M.chead[j];q->down!=NULL&&q->down->x<i ;q=q->down  );
            p->down=q->down;
            q->down=p;
        }
    }
}
void query(int k)
{
    int flag=0,i=0;
    while(flag==0&&i<=n)
    {
        ++i;
        node *p;
        p=M.rhead[i]; 
        while(p!=NULL)
        {
            if(p->w==k){
                flag=1;
                cout<<p->x<<" "<<p->y;
                break;  
            }
            else p=p->right; 
        }
    }
    if(flag==1) ;
    else cout<<"ERROR";
}
int main()
{
    create();
    int n;
    cin>>n;
    query(n);
    
    return 0;
}

 

The goal set by the last blog was not accomplished because the lecture was too fast to do + the thought understood but the code could not be knocked out.

The next goal is to familiarize yourself with string functions and knock out the chairman tree.

Posted by ramjai on Thu, 09 May 2019 18:44:40 -0700