[Ybt OJ] [String Algorithm Chapter 1] String Processing

「 「 String Algorithm」」" No.1 1Chapter 1 String Processing
Directory:

A.数字反转
B.位移包换
C.单词替换
D.字符串环
E.生日相同

This question is too shabby...

A . A. A . Example111 number inversion

Insert picture description here

analysis:

You can directly use the function strrev strrevs t r r e v do it again0 00
can also bedirectly converted

CODE:

#include<iostream>
#include<cstdio>
#pragma GCC optimize(2)
using namespace std;
int numa,numb;
int main(){
    
    
    for(scanf("%d",&numa);numa!=0;numa/=10)
        numb=numb*10+numa%10;
    printf("%d",numb);
    return 0;
} 

B . B. B . Example222 displacement replacement

Insert picture description here

analysis:

A cough question Just copy
the longest string and find the short one .

CODE:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> 
//#pragma GCC optimize(2)
#define reg register 
using namespace std;
typedef long long ll;
typedef double db;
string s1,s2;
int main(){
    
    
	cin>>s1>>s2;
	if(s1.length()<s2.length()) swap(s1,s2);  //找最长
	s1+=s1;  //复制
	if(s1.find(s2)==-1) puts("false");  //查找
	else puts("true");	
	return 0;
} 

C . C. C . Example333 word replacement

Insert picture description here

analysis:

In fact, you can enter each word separately and then directly judge the equality and replace it .

CODE:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> 
#pragma GCC optimize(2)
#define reg register 
using namespace std;
typedef long long ll;
typedef double db;
string s[101],a,b;
void work(int x)
{
    
    
	if(a==s[x]) cout<<b<<" ";  //匹配 替换
	else cout<<s[x]<<" ";  //原样输出
}
int main(){
    
    
	int n=0;
	char ch;
	do{
    
    
		cin>>s[++n];//cout<<"XXX ";
		scanf("%c",&ch);
	}while(ch==' ');
	cin>>a>>b;
	for(reg int i=1;i<=n;i++)
		work(i);
	return 0;
} 

D . D. D . Example444 string loop

Insert picture description here

analysis:

Can mod modm o d string lengthto update thematching position,you don't need tocopytwo strings directly.

CODE:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> 
//#pragma GCC optimize(2)
#define reg register 
using namespace std;
typedef long long ll;
typedef double db;
string s1,s2;
int ans,cnt;
int main(){
    
    
	cin>>s1>>s2;
	//s1+=s1;
	//s2+=s2;
	//if(s1.length()<s2.length()) swap(s1,s2);
	int minlen=min(s1.length(),s2.length());
	for(int i=0;i<s1.length();i++)
		for(int j=0;j<s2.length();j++)
		{
    
    
			int k=i,l=j;
			cnt=0;
			while(s1[k]==s2[l])  //公共
			{
    
    
				cnt++;
				k=(k+1)%s1.length();  //向后更新
				l=(l+1)%s2.length();
				if(cnt>=minlen) break;
			}
			ans=max(ans,cnt);
		}
	printf("%d",ans);
	return 0;
} 

IS . IS.E . Example55Same birthday on 5

Insert picture description here
Insert picture description here

analysis:

As for the consideration of equality, it is better to directly consider inequality and then continue to compare and judge.
Therefore, if it is not the same month and the same day , directly wrap and re-output a line, and the
rest will follow the output name . Note sort sortsort
p s : ps: p s : Don't usechar charc h a r array withstring stringstring

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100005
using namespace std;
struct st{
    
    
    int month,day;
    string name;
    bool operator<(const st& a)const{
    
    
        if(month!=a.month) return month<a.month;
        if(day!=a.day) return day<a.day;  //排序
        if(name.length()!=a.name.length()) return name.length()<a.name.length();  
        return name<a.name;
    }
}a[MAXN];
int n,next_month,next_day,k;
int main()
{
    
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    
    
    	cin>>a[i].name;
    	scanf("%d%d",&a[i].month,&a[i].day);
	}
    sort(a+1,a+n+1);
    next_month=a[1].month,next_day=a[1].day;
    for(int i=2;i<=n;i++)
    {
    
    
        if(a[i].month!=next_month||a[i].day!=next_day)
        {
    
    
            next_month=a[i].month;
            next_day=a[i].day;
        }else k++;  //有相同生日
    }
    if(k)
    {
    
    
        printf("%d %d ",a[1].month,a[1].day);
        cout<<a[1].name<<" ";
        next_month=a[1].month,next_day=a[1].day;
        for(int i=2;i<=n;i++)
        {
    
    
            if(a[i].month!=next_month||a[i].day!=next_day)  //不是同生日
            {
    
    
            	printf("\n");
                next_month=a[i].month;
                next_day=a[i].day;
                printf("%d %d ",next_month,next_day);  //重新输出一行
                cout<<a[i].name<<" ";
            }
            else cout<<a[i].name<<" ";  //跟着输出
        }
    }else puts("None");
    return 0;
}

Guess you like

Origin blog.csdn.net/dgssl_xhy/article/details/112393608