寒假笔记·STL一时爽

map的用法

详细讲解
字符串数组不可以做map的键值!!!

set的用法

详细讲解

string的操作

string用法总结:详细讲解

例题:P1628 合并序列

原题地址
题目描述

有N个单词和字符串T,按字典序输出以字符串T为前缀的所有单词。

输入输出格式

输入格式:
输入文件第一行包含一个正整数N;

接下来N行,每行一个单词,长度不超过100;

最后一行包含字符串T。

【数据规模】

对于60%的数据,满足1≤N≤1000;

对于100%的数据,满足1≤N≤100000且所有字符均为小写字母;

输出格式:
按字典序升序输出答案。

输入输出样例

输入样例#1:
6
na
no
ki
ki
ka
ku
k
输出样例#1:
ka
ki
ki
ku

代码


#include <iostream>
#include <algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int n;
string k,a[1000005]; 

int main()
{
    int i;
    cin>>n;
    for(i=1;i<=n;i++)
       cin>>a[i];
    cin>>k;
    sort(a+1,a+n+1);
    for( i=1;i<=n;i++) 
       if(a[i].find(k)==0)cout<<a[i]<<endl;
    return 0;
}

string.find()

例题:Sasha and One More Name

知识点:string.substr();

知识点

原题地址

Reading books is one of Sasha’s passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: “Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not.”

And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as “kazak”, “oo” and “r” are palindromes, but strings “abb” and “ij” are not.

Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn’t be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times k, so they got k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn’t be turned over, they could be shuffled.

In this way, it’s possible to achive a string abcdefg from the string f|de|abc|g using 3 cuts (by swapping papers with substrings f and abc). The string cbadefg can’t be received using the same cuts.

More formally, Sasha wants for the given palindrome s find such minimum k, that you can cut this string into k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won’t be equal to the initial string s. It there is no answer, then print “Impossible” (without quotes).

Input
The first line contains one string s (1≤|s|≤5000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that ss is a palindrome.

Output
Print one integer k — the minimum number of cuts needed to get a new name, or “Impossible” (without quotes).

Examples
input
nolon
output
2
input
otto
output
1
input
qqqq
output
Impossible
input
kinnikkinnik
output
1
Note
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.

In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.

In the third example, you can’t make a string, that won’t be equal to the initial one.

In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.

代码:
分析可知,最多只需分两次。可以使用string.substr()函数判断是否可以一次解决问题。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s,t;
	cin>>s;
	int flag=1;
	for(int i=0;i<s.length()/2;i++)
		if(s[i]!=s[0])
		{
			flag=0;
			break;
		}
	if(flag)
	{
		printf("Impossible\n");
		return 0;
	}
		
	else{
		for(int i=1;i<s.length();i++)
		{
			t=s.substr(i)+s.substr(0,i);
			if(s!=t)
			{
				flag=1;
				for(int i=0;i<t.length()/2;i++){
					if(t[i]!=t[t.size()-i-1]){
						flag=0;
						break;
					}
				}
				if(flag==1)
				{
					printf("1\n");
					return 0;
				}
					
			}
		} 
		printf("2\n");
		return 0; 
	}
}

vector

例题:

题目描述

有一个沿海地区,可以看作有n行m列的城市,第i行第j列的城市海拔为h[i][j]。
由于沿海,所以这个地区经常会发生海啸。
海啸发生时,部分城市会被淹没,具体来说,海水高度会达到d,因此海拔低于d的城市都会被淹没。
现在有q次询问,每次问你一个矩形区域中,有多少城市不会被淹没。
输入描述:

第一行三个整数n,m,d,具体含义见题目描述。
接下来n行,每行m个整数,其中第i行第j列的整数为h[i][j],具体含义见题目描述。
第n+2行一个整数q,表示询问数。
接下来q行,每行四个整数a,b,x,y,
表示询问从第a行第b列到第x行第y列的矩形地区中,有多少地区不会被淹没。
即有多少个i,j,满足 a ≤ i ≤ x , b ≤ j ≤ y ,且 h[i][j] ≥ d 。
输出描述:

共q行,第i行一个整数,表示第i个询问的答案。
示例1

输入
3 3 3
1 2 3
2 1 5
4 3 2
2
1 2 2 3
2 1 3 3
输出
2
3
备注:

1 ≤ n×m ≤ 106
1 ≤ q ≤ 105
0 ≤ d , h[i][j] ≤ 109
代码:
因为题目仅告知1 ≤ n×m ≤ 106,所以数组要动态申请,可用vector实现。
vector< type> v (size,value);

//大小为n的一维数组,且初始化为0
vector<int> v(n,0)

//大小为n×m的二维数组,且初始化为0
vector<vector<int> > v(n,vector<int>(m,0))

以下为完整代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{	
	int n,m,d,t;
	scanf("%d %d %d",&n,&m,&d);
	vector<vector<int> > s(n+10,vector<int>(m+10,0));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%d",&t);
			s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
            if(t>=d)
                s[i][j]++;
		}
	}
	int q,a,b,x,y;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d %d %d %d",&a,&b,&x,&y);
		int ans=s[x][y]-s[x][b-1]-s[a-1][y]+s[a-1][b-1];
		printf("%d\n",ans);
	}
	return 0;
}

next_permutation(qq,qq+n)

例题:P1088 火星人

原题地址
题目描述

人类终于登上了火星的土地并且见到了神秘的火星人。人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法。这种交流方法是这样的,首先,火星人把一个非常大的数字告诉人类科学家,科学家破解这个数字的含义后,再把一个很小的数字加到这个大数上面,把结果告诉火星人,作为人类的回答。

火星人用一种非常简单的方式来表示数字――掰手指。火星人只有一只手,但这只手上有成千上万的手指,这些手指排成一列,分别编号为1,2,3…1,2,3…。火星人的任意两根手指都能随意交换位置,他们就是通过这方法计数的。

一个火星人用一个人类的手演示了如何用手指计数。如果把五根手指――拇指、食指、中指、无名指和小指分别编号为1,2,3,41,2,3,4和55,当它们按正常顺序排列时,形成了55位数1234512345,当你交换无名指和小指的位置时,会形成55位数1235412354,当你把五个手指的顺序完全颠倒时,会形成5432154321,在所有能够形成的120120个55位数中,1234512345最小,它表示11;1235412354第二小,它表示22;5432154321最大,它表示120120。下表展示了只有33根手指时能够形成的66个33位数和它们代表的数字:

三进制数

123
132
213
231
312
321
代表的数字

1
2
3
4
5
6

现在你有幸成为了第一个和火星人交流的地球人。一个火星人会让你看他的手指,科学家会告诉你要加上去的很小的数。你的任务是,把火星人用手指表示的数与科学家告诉你的数相加,并根据相加的结果改变火星人手指的排列顺序。输入数据保证这个结果不会超出火星人手指能表示的范围。

输入输出格式

输入格式:
共三行。
第一行一个正整数N,表示火星人手指的数目(1≤N≤10000)。
第二行是一个正整数M,表示要加上去的小整数(1≤M≤100)。
下一行是1到N这N个整数的一个排列,用空格隔开,表示火星人手指的排列顺序。

输出格式:
NN个整数,表示改变后的火星人手指的排列顺序。每两个相邻的数中间用一个空格分开,不能有多余的空格。

输入输出样例

输入样例#1:
5
3
1 2 3 4 5
输出样例#1:
1 2 4 5 3
说明

对于30%的数据,N≤15;

对于60%的数据,N≤50;

对于全部的数据,N≤10000;
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int qq[10010],n,m;
int main()
{
    int i;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
        scanf("%d",&qq[i]);
    while(m--)
    {
        next_permutation(qq,qq+n);
    }
    for(i=0;i<n-1;i++)
        printf("%d ",qq[i]);
    printf("%d\n",qq[n-1]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值