PAT字符串处理

字符串处理

1)string 80%

  1. char[] 20%

格式化A + B :(PAT1001)

1)a+b

2)转化成字符串

3)用逗号分成每三个一组

#include <iostream>

using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    int c=a+b;
    string num=to_string(c); //数字转换成字符串
    string res;
    for(int i=num.size()-1,j=0;i>=0;i--)
    {
        res=num[i]+res; //在前面加数字
        j++;
        if(j%3==0 &&i && num[i-1]!='-') res=','+res;
    }
}

拼写正确:(PAT1005)

输入N,计算N的各位数字之和S,将S的每一位用英文单词表示。

N  - 10^100 
string N ;
1)扣出每一位,计算和S
2)


#include <iostream>
using namespace std;

int main()
{
    string n;
    cin>>n;
    
    int s=0;
    for(auto c:n)
    {
        s+=c-'0';  //计算每一位的总和
    }
    string str=to_string(s);
    
    char word[10][10]={
        "zero","one","two","three","four","five",
        "six","seven","eight","nine"
    };
    cout<<word[str[0]-'0'];
    for(int i=1;i<str.size();i++) cout<<' '<<word[str[i]-'0'];
    
    return 0;
    
}

签到和迁出:

1)怎么存数据

2)怎么处理数据

3
CS301111 15:30:28 17:00:10  //id   签入的时间  签出时间
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

字符串求最小值,最大值。 每次要记录俩个值(id ,签入最小的时间)

时间比较大小:

1)先比较小时

2)再比较分钟

位数相同—>可以比较字典序

#include <iostream>

using namespace std;
int main()
{
    string op_id,open_time;
    string close_id,close_time;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        string id,in_time,out_time;
        cin>>id>>in_time>>out_time;
        //更新开门的时间
        if(!i || in_time<open_time)
        {
            open_id=id;
            open_time=in_time;
        }
        //更新关门的时间
        if(!i || out_time>close_time)
        {
            close_id=id;
            close_time=out_time;
        }
    }
    cout<<op_id<<' '<<open_id<<endl;
    
    return 0;
}

密码:

1)需要存储的数据: 需要修改的同学的用户名,修改后的同学的密码是什么,需要修改密码的同学的个数n。

2)

#include <iostream>
using namespace std;
const int N=1010;
string name[N],pwd[N];
string change(string str)
{
    string res;
    for(auto c:str)
    {
        if(c=='1') res+='@';
        else if(c=='0') res+='%';
        else if(c=='l') res+='L';
        else if(c=='o') res+='O';
        else 
        {
            res+='c';
        }
    }
    return res;
}

int main()
{
    int n;
    cin>>n;
    int m=0;
    for(int i=0;i<n;i++)
    {
        string cur_name,cur_pwd;
        cin>>cur_name>>cur_pwd;
        
        string change_pwd=change(change_pwd);
        if(change_pwd!=cur_pwd)
        {
            name[m]=cur_name;
            pwd[m]=change_pwd;
            m++;
        }
        if(!m)
        {
            printf("There are %d accounts and no account is modified\n",n);
        }
        else 
        {
            cout<<m<<endl;
            for(int i=0;i<m;i++)
                cout<<name[i]<<' '<<pwd[i]<<endl;
        }
        
    }
       
    return 0;
    
}

男生和女生:

1)女生第一名的姓名和ID,分数(求差值)

2)男生倒数第一名的姓名,ID和分数

对于每个同学:

​ if 是男生 :看成绩

存储:姓名 (string) id (string) 分数(int):

不存在 :string 为空

模拟题:凡是只有一种方案,按照一定的步骤就能模拟出来

1)怎么存数据

2)怎么算结果

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin>>n;
	
	string girl_name,girl_id; //存储女生信息
	int girl_score;
	string boy_name,boy_id;  //存储男生信息
	int boy_score;
	
	for(int i=0;i<n;i++)
	{
		string name,sex,id;
		int score;
		cin>>name>>sex>>id>>score;
		
		if(sex=="F") //更新女生信息 
		{
			if(girl_name.empty() || girl_score<score)
			{
				girl_name=name;
				girl_id=id;
				girl_score=score;
			}
		}
		else //更新男生信息 
		{
			if(boy_name.empty() || boy_score >score)
			{
				boy_name=name;
				boy_id=id;
				boy_score=score;
			}
		}
		
	}
	
	//如果女生不存在 
	if(girl_name.empty()) puts("Absent");
	else cout<<girl_name<<' '<<girl_id <<endl;
	
	//如果男生不存在 
	if(boy_name.empty()) puts("Absent");
	else cout<<boy_name<<' '<<boy_id<<endl;
	
	if(girl_name.size() && boy_name.size())  cout<<abs(girl_score-boy_score)<<endl;
	else cout<<"NA"<<endl;
	
	return 0;
	
}

字符串减法:(删除S1中在S2中出现的字符)

字符串处理:

删除可以改成添加: 1)先定义一个空串,把不删除的加进来。

如何输入一个包含空格的字符串:

string s1;

getline(cin,s1);

判断某个元素是否在某个几何中出现过?S1 在 S2中出现过。----->哈希表

哈希表: O(1)

​ 1)插入

​ 2)删除

​ 3)修改

​ 4)查找

哈希表不需要自己写:unordered_set<char> ;
用哈希表优化:
#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    string s1;
	string s2;
    getline(cin,s1);
     getline(cin,s2);
    
    unordered_set<char>  hash; //定义hash表
	for(auto c:s2) hash.insert(c); //将s2中的字符插入hash表
    string  res;
	for(auto c: s1)
    	if(!hash.count(c))
           res+=c;
	cout<<res<<endl;
    return 0;
}

hash表的用法:

​ 1)hash表的定义: unordered_set hash;

​ 2)hash表的插入:for(auto c:s2) hash.insert©;

3)hash表的查找:for(auto c: s1)
	if(!hash.count(c))
       res+=c;

map 和 set 的区别:

#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>

//基于平衡树实现:O(logn)
//有序
set<int>  a;  //维护一个有序集合
map<int,int> b; //维护一个映射
multiset<int>  c; //可以重复

//基于hash表:  O(1)
//无序
unordered_set<int > e;
unordered_map<int,int> f;

说话方式: 求最长用的单词及出现的次数(不区分大小写)---->可以将大写转换成小写

做法:1)要将每个单词抠出来 :可以用getline(cin, s1) 读取整行

​ 2)提取每个单词 ,双指针算法

for(int i=0;i<str.size();i++)
    if(str[i]是字母或数字)
        while()
        {
            j=i;
            直到j不为字母或数字位置
            最后让:i=j;
        }
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值