代码:
#include
using namespace std;
int main(){
int ans = 0, pre = -1, a, n;
cin >> n;
while(n --){
cin >> a;
//第一次遍历 pre = -1 会导致多加1 ,但是数组末尾的那一段少计算了,所以抵消了
if(pre != a)
++ ans, pre = a;
}
cout << ans << endl;
return 0;
}
代码:
#include
using namespace std;
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap(int year){
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
int main(){
int y, d, i = 1, j = 1;
cin >> y >> d;
for(; i <= 12; i ++){
int cnt = days[i];
if(i == 2 && isLeap(y))
cnt = 29;
for(j = 1; j <= cnt; j ++){
d --;
if(!d)
break;
}
if(!d)
break;
}
cout << i << endl << j;
return 0;
}
11 2
<html>
<head>
<title>User {{ name }}title>
head>
<body>
<h1>{{ name }}h1>
<p>Email: <a href="mailto:{{ email }}">{{ email }}a>p>
<p>Address:{{ address }}p>
body>
html>
name "David Beckham"
email "[email protected]"
样例输出
<html>
<head>
<title>User David Beckhamtitle>
head>
<body>
<h1>David Beckhamh1>
<p>Email: <a href="mailto:[email protected]">[email protected]a>p>
<p>Address: p>
body>
html>
评测用例规模与约定
0 ≤ m ≤ 100
0 ≤ n ≤ 100
输入的模板每行长度不超过 80 个字符(不包含换行符)。
输入保证模板中所有以 {{ 开始的子串都是合法的标记,开始是两个左大括号和一个空格,然后是变量名,结尾是一个空格和两个右大括号。
输入中所有变量的值字符串长度不超过 100 个字符(不包括双引号)。
保证输入的所有变量的名字各不相同。
题解:
首先把每一行句子存到vector
代码:
#include
#include
#include
using namespace std;
string arr[110];
unordered_map<string, string> map;
int main(){
int n, m;
string a, b;
cin >> n >> m;
getchar();
//存储句子
for(int i = 0; i < n; i ++)
getline(cin, arr[i]);
while(m --){
cin >> a;
getline(cin, b);
//存储VAR对应的字符串
map[a] = b.substr(2, b.length() - 3);
}
//遍历每一个句子
for(int i = 0; i < n; i ++){
string tmp = arr[i];
for(int j = 0; j < tmp.length(); j ++){
//遇到了VAR
if(j + 1 < tmp.length() && arr[i][j] == '{' && arr[i][j + 1] == '{'){
int t = j + 3;
//s存储VAR是什么字符串
string s;
while(t < arr[i].length() && arr[i][t] != ' ')
s += tmp[t ++];
//需要跳过 " }}" 多于字符
j = t + 2;
//输出VAR对应的字符串
cout << map[s];
}else{ //不是VAR就原样输出
cout << tmp[j];
}
}
cout << endl;
}
return 0;
}
代码:
#include
#include
#include
using namespace std;
const int N = 10010, M = 100010;
vector<int> G[N];
int stk[M], num[M], low[M], scc[M], top = 0, cnt = 0, ans = 0, dfn = 0;
int n, m;
void dfs(int u){
low[u] = num[u] = ++dfn;
stk[top ++] = u;
for(int v : G[u]){
//没有遍历过
if(!num[v]){
dfs(v);
low[u] = min(low[u], low[v]);
//已经访问了,需要回溯
}else if(!scc[v]){
low[u] = min(low[u], num[v]);
}
}
if(num[u] == low[u]){
++ cnt;
int tmp = 0;
while(true){
//栈前面的都是属于一个强连通分量
int v = stk[-- top];
scc[v] = cnt;
++ tmp;
if(v == u)
break;
}
ans += tmp * (tmp - 1) / 2;
}
}
int main(){
int a, b;
cin >> n >> m;
while(m --){
cin >> a >> b;
G[a].push_back(b);
}
for(int i = 1; i <= n; i ++)
if(!num[i])
dfs(i);
cout << ans;
return 0;
}
代码:
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 110;
const LL INF = 1e18;
int n;
LL m;
int tr[N][26], cnt[N], ne[N], idx;
int q[N];
LL ans[N][N], w[N][N];
void insert(char* str)
{
int p = 0;
for (int i = 0; str[i]; i ++ )
{
int u = str[i] - 'a';
if (!tr[p][u]) tr[p][u] = ++ idx;
p = tr[p][u];
}
cnt[p] ++ ;
}
void build()
{
int hh = 0, tt = -1;
for (int i = 0; i < 26; i ++ )
if (tr[0][i])
q[ ++ tt] = tr[0][i];
while (hh <= tt)
{
int t = q[hh ++ ];
for (int i = 0; i < 26; i ++ )
{
int p = tr[t][i];
if (!p) tr[t][i] = tr[ne[t]][i];
else
{
ne[p] = tr[ne[t]][i];
cnt[p] += cnt[ne[p]];
q[ ++ tt] = p;
}
}
}
}
void mul(LL c[][N], LL a[][N], LL b[][N])
{
static LL tmp[N][N];
memset(tmp, -0x3f, sizeof tmp);
for (int i = 0; i <= idx; i ++ )
for (int j = 0; j <= idx; j ++ )
for (int k = 0; k <= idx; k ++ )
tmp[i][j] = max(tmp[i][j], a[i][k] + b[k][j]);
memcpy(c, tmp, sizeof tmp);
}
int main()
{
cin >> n >> m;
char str[N];
while (n -- )
{
cin >> str;
insert(str);
}
build();
memset(w, -0x3f, sizeof w);
for (int i = 0; i <= idx; i ++ )
for (int j = 0; j < 26; j ++ )
{
int k = tr[i][j];
w[i][k] = max(w[i][k], (LL)cnt[k]);
}
for (int i = 1; i <= idx; i ++ ) ans[0][i] = -INF;
while (m)
{
if (m & 1) mul(ans, ans, w);
mul(w, w, w);
m >>= 1;
}
LL res = 0;
for (int i = 0; i <= idx; i ++ ) res = max(res, ans[0][i]);
cout << res << endl;
return 0;
}