标签:周赛 遍历 23 int 题解 cin long ans mod
1.小L的作文
链接:https://ac.nowcoder.com/acm/contest/11164/A
来源:牛客网
小 L 刚考完期末,他写了一篇很烂的作文,烂到老师都不愿意给它扣分,只能给他加分,已知老师比较牛,所以他发现一个字符 x 就会加一分。问你小 L 最后可以得到多少分。
输入描述:
第一行,给你一个字符 x,表示可以加分的字符。
第二行,给你一个字符串 s,表示文章。
输出描述:
一个整数,表示小 L 得了多少分。
#include <iostream>
using namespace std;
int main() {
char x;
string s;
cin >> x >> s;
int ans=0;//答案
for(auto t:s) ans += t==x;//用t遍历s
cout << ans;
return 0;
}
总结:
题目很简单,但是遍历string的方法需要学习一下,更加简单。
auto遍历,智能指针yyds,自动识别类型。
t==s 很巧妙,等于的话就加一,不等的话加零。
auto遍历适用于所有的遍历,只不过这种遍历是遍历完数据,当要遍历部分数据时就要换一种方法。
auto遍历举例:
数组:
int s[4];
for(int i=0;i<4;i++) cin>>s[i];
for(auto t:s) //用t遍历s
cout << t;
map:
map<int ,int>mp;
for(auto i : mp)
cout<<i.first<<i.second<<"\n";
set:
set<int>se;
for(suto i:se)
cout<<i;
2.小L的多项式
刚开始我就是无脑做的,连想都没想,一看快速幂,就直接快速幂了。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353 ;
int n,m;
int a[1005],x;
LL fast(LL a,LL b)
{
LL res = 1;
while(b>0)
{
if(b&1) res = res*a%mod;
b>>=1;
a=a*a%mod;
}
return res;
}
//龟速乘
long long quick_mul(long long x,long long y)
{
long long ans=0;
while(y!=0){
if(y&1)
ans = (ans+x)%mod;
//ans+=x,ans%=mod;
x = x*2%mod;
//x=x+x,x%=mod;
y>>=1;
}
return ans;
}
int main()
{
cin>>n;
for(int i=0;i<=n;i++) cin>>a[i];
cin>>m;
for(int i=1;i<=m;i++)
{
LL ans = 0;
cin>>x;
ans+=a[0];
for(int j=1;j<=n;j++)
{
ans += (a[j]*fast(x,j))%mod;
ans %= mod;
}
cout<<ans<<" ";
}
return 0;
}
但其实再看式子可以发现规律:
#include <iostream>
#define mod 998244353
using namespace std;
int a[10005];
int main() {
int n;
cin >> n;
for(int i=0;i<=n;i++) cin >> a[i];
int q;
cin >> q;
while(q--)
{
int x;
cin >> x;
long long ans=0;//long long
for(int i=n;i>=0;i--) ans=(ans*x+a[i])%mod;//每次*x+a[i]
cout << ans << " ";
}
return 0;
}
3.小L的编辑器
L 后缀 abd -> 要反转
R 前缀 ce -> 不需要反转
#include <iostream>
#include <algorithm>
using namespace std;
string a,b,x,y;//x后缀,y前缀
int main() {
cin >> a >> b;
int n=a.size();
for(int i=0;i<n;i++)
{
if(b[i]=='L') x+=a[i]; //放到左边相当于后缀多了个a[i]
else y+=a[i]; //右边相当于前缀多了个a[i]
}
reverse(x.begin(),x.end());//后缀要反过来
cout << y << x;
return 0;
}
自己写的方法,模拟
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
char ch1[1000005];
char ch2[1000005];
int main()
{
cin>>s1>>s2;
int r = 1,l=1;
for(int i=0;i<s1.length();i++)
{
if(s2[i]=='L')ch1[r++] = s1[i];
else ch2[l++] = s1[i];
}
for(int i=1;i<l;i++) cout<<ch2[i];
for(int i=r-1;i>=1;i--) cout<<ch1[i];
return 0;
}
其实思路差不多一样
4. 小L的数列
dp问题,自己还是不太会,水平也只到这里了,原谅自己太菜了。
建议做一下最长公共子序列的问题。
标签:周赛,遍历,23,int,题解,cin,long,ans,mod 来源: https://blog.csdn.net/qq_50285142/article/details/114415644
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。