st

字符串方法汇总,大全。python day 6

守給你的承諾、 提交于 2020-01-10 09:40:35
# String的内置方法 # st='hello kitty {name} is {age}' # # print(st.count('l')) # 统计元素个数 # print(st.capitalize()) # 首字母大写 # print(st.center(50,'#')) # 居中 # print(st.endswith('tty3')) # 判断是否以某个内容结尾 # print(st.startswith('he')) # 判断是否以某个内容开头 # print(st.expandtabs(tabsize=20)) # print(st.find('t')) # 查找到第一个元素,并将索引值返回 # print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{} # print(st.format_map({'name':'alex','age':22})) # print(st.index('t')) # print('asd'.isalnum()) # print('12632178'.isdecimal()) # print('1269999.uuuu'.isnumeric()) # print('abc'.isidentifier()) # print('Abc'.islower()) # print(

503. Next Greater Element II

江枫思渺然 提交于 2020-01-09 00:27:15
https://leetcode.com/problems/next-greater-element-ii/description/ class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { stack<int> st; int n = nums.size(); for (int i = 2 * n - 1; i >= n; i--) { int cur = nums[i % n]; while (!st.empty() && cur >= st.top()) st.pop(); st.push(cur); } vector<int> res(n, 0); for (int i = n - 1; i >= 0; i--) { int cur = nums[i]; while (!st.empty() && cur >= st.top()) st.pop(); if (st.empty()) res[i] = -1; else res[i] = st.top(); st.push(cur); } return res; } }; 来源: https://www.cnblogs.com/JTechRoad/p/8990477.html

Codeforces Round #611 (Div. 3) C:Friends and Girls

霸气de小男生 提交于 2020-01-08 15:04:53
题目链接 题意: 送礼物,每个人都要送给别人礼物和收到别人礼物,一开始给你个序列,已知其中一些序列,然后求满足情况的完整序列 题解 这个题是个思路题,唉,我以为是构造环,写了一个 bfs() ,令出度入度匹配,然而不幸TLE,真正的思路是,点只要不在原位置,就可以 TLE代码: #include < bits / stdc ++ . h > using namespace std ; #define ll long long const int maxn = 2e5 + 5 ; int in [ maxn ] , out [ maxn ] , vis [ maxn ] , n ; void bfs ( ) { queue < int > q ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! out [ i ] && ! in [ i ] ) { q . push ( i ) ; } } while ( ! q . empty ( ) ) { int st = q . front ( ) ; q . pop ( ) ; if ( out [ st ] || in [ st ] ) continue ; for ( int i = 1 ; i <= n ; i ++ ) { if ( st != i && ! in [ i ] ) { out [

CDOJ 1061 C - 秋实大哥与战争 STL set 迭代器

时光总嘲笑我的痴心妄想 提交于 2020-01-05 02:50:47
题目链接: C - 秋实大哥与战争 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit Status Practice UESTC 1061 Appoint description: System Crawler (2016-04-23) Description 男儿何不带吴钩,收取关山五十州。 征战天下是秋实大哥一生的梦想,所以今天他又在练习一个对战游戏。 秋实大哥命令所有士兵从左到右排成了一行来抵挡敌人的攻击。 敌方每一次会攻击一个士兵,这个士兵就会阵亡,整个阵列就会从这个位置断开;同时有的时候已阵亡的士兵会受人赢气息感染而复活。 秋实大哥想知道某一时刻某一个士兵所在的阵列的长度是多少。 Input 第一行包含两个整数 n , m ,表示秋实大哥的士兵数目和接下来发生的事件数目。 接下来 m 行,每一行是以下三种事件之一: 0 x : 表示x位置的士兵受到攻击阵亡 1 x : 表示x位置的士兵受人赢气息感染复活 2 x : 秋实大哥想知道第x个士兵所在阵列的长度 1 ≤ n , m ≤ 100000 , 1 ≤ x ≤ n 。 Output 对于每一个 2 x 事件,输出对应的答案占一行。 Sample Input 5 3 2 2 0 3 2 2 Sample Output 5

Count on a tree II

笑着哭i 提交于 2020-01-03 06:41:26
You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent the weight of nodes there are on the path from u to v. Input In the first line there are two integers N and M. (N <= 40000, M <= 100000) In the second line there are N integers. The i-th integer denotes the weight of the i-th node. In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v). In the next M lines, each line contains two integers u v,

(栈,BST) leetcode 224. Basic Calculator 173. Binary Search Tree Iterator

旧巷老猫 提交于 2020-01-02 19:43:52
开两个栈,一个记录数字,一个记录操作符。 然后从前往后扫描整个表达式: 1. 如果遇到 (、+、-,直接入栈; 2. 如果遇到数字,则判断操作符栈的栈顶元素,如果不是 (,则弹出操作符的栈顶元素,并用相应操作更新数字栈的栈顶元素。从而保证操作符栈的栈顶最多有一个连续的+或-; 3. 如果遇到 ),此时操作符栈顶一定是 (,将其弹出。然后根据新栈顶的操作符,对数字栈顶的两个元素进行相应操作; 时间复杂度分析:每个数字和操作进栈出栈一次,所以总时间复杂度是 O(n) class Solution { public: void calc(stack<char>& op, stack<int>& num){ int y = num.top(); num.pop(); int x = num.top(); num.pop(); if(op.top() == '+') num.push(x+y); else num.push(x-y); //这里一定是先进栈的x减去后进栈的y op.pop(); } int calculate(string s) { //s 中包含 ( ) + - 非负整数 空格 // two stacks: one records numbers, one records operations stack<int> num; stack<char> op; for(int

栈 队列 优先队列 STL

余生颓废 提交于 2020-01-01 14:36:00
栈 #include<stack> //头文件 stack<char> st; //定义 st.push(str1[0]); //入栈 cur=st.top (); //取栈顶值 st.pop(); //出栈 st.empty ()//空为true 队列 #include<queue> queue<char>que; que.push(a); a=que.front(); que.pop(); que.empty ()优先队列 bool operator < (const coor &a, const coor &b) { return a.time > b.time; //从小到大排序,修改为最小堆,取最小值。 } priority_queue<coor>que; que.push(a); a=que.top();// 默认为最大堆,取最大值。 que.pop(); que.empty () 以下对vector、list、deque容器通用,这里以list容器为例,注意访问元素时vector、deque可用下标直接访问,但对list不行 list<line> dlist; vector<int> c; line in; list<line>::iterator iter;//定义一个指向元素的迭代器 插入: dlist.insert(iter,t)/

MFC获取时间字符串

爷,独闯天下 提交于 2020-01-01 13:04:29
基本上有2种方式,一种是利用"time.h"文件中的系统函数;另一种是利用CTime类。 利用系统函数。 # include "time.h" CString time_cstr ; SYSTEMTIME st ; //定义系统时间结构体的对象 GetLocalTime ( & st ) ; //调用GetLocalTime获得当前时间,并保存在结构体st中 time_cstr . Format ( _T ( "%04d-%02d-%02d %02d:%02d:%02d:%3d" ) , st . wYear , st . wMonth , st . wDay , st . wHour , st . wMinute , st . wSecond , st . wMilliseconds ) ; 利用CTime类。 CString MyGetCurrenTime ( ) { CTime time = CTime : : GetCurrentTime ( ) ; CString curTime ; curTime . Format ( L "%04d-%02d-%02d %02d:%02d:%02d" , time . GetYear ( ) , time . GetMonth ( ) , time . GetDay ( ) , time . GetHour ( ) , time .

Python 实现strip/Java trim 方法

一世执手 提交于 2019-12-24 12:29:54
刚学习python中 ,想用python代码实现strip 方法 实现思路,定义两个变量 a1,a2 循环入参字符串, a1从字符串前面循环找到第一个不为空格的字符位置, a2则从后面循环找到最后一个不为空格的字符串位置.然后截取字符串即可. 上代码: def trim(st): if not isinstance(st, str): raise TypeError('not a str') i = 0 while i < len(st): if st[i] == ' ': i = i + 1 else: break if i == len(st): return ' ' j = len(st) - 1 while j >= 0: if st[j] == ' ': j = j - 1 else: break j = j + 1 return st[i:j] 来源: CSDN 作者: 不闻不问兮 链接: https://blog.csdn.net/keke_yuer/article/details/103679767

POJ 1716

旧时模样 提交于 2019-12-24 02:16:21
题意:在几个区间里面,挑选出几个数字组成一个集合,使得每个区间都至少有两个数字在这个集合里面,求这个集合的最少数字个数。 题解:贪心法,先对区间排序下,以右端点排序,把每个区间扫一遍过去,看区间内有几个元素在当前集合中。 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cctype> 6 #include <time.h> 7 #include <string> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <vector> 12 #include <stack> 13 #include <algorithm> 14 #include <iostream> 15 using namespace std; 16 #define PI acos( -1.0 ) 17 typedef long long ll; 18 typedef pair<int,int> P; 19 const double E = 1e-8; 20 21 const int NO = 10000 + 5; 22 int a[NO<<1]; 23 int n; 24 struct ND 25 {