有参加今晚360前端笔试的吗?最后一道编程题我连意思都没看懂

题目:字符置换,字符串中出现的连续两个“.”。若每次操作把其中最开始的连续两个“.”以一个“.”替代,则可以将函数f(s) 定义为 使得串中不出现连续两个“.”的最小置换次数。  现考虑m个字符替换操作,每次将指定位置的字符替换为给定的字符,求替换后函数f(s)的值。

 * 输入描述:输入有若干组,每组的第一行为两个整数n和m,1<= n,m<=300000,表示字符串的长度和字符替换操作的次数。
 * 第二行为所给的字符串,随后紧跟m行操作,每行由一个正整数x和一个字母c构成,表示将字符串中位置m处的字符置换为字母c。
 * 输出描述:对每组输入的每个置换操作,在单独的行中输出函数f(s)的结果
#360公司##前端工程师#
全部评论
这个很简单啊,就是让你判断一个字符串 消除多少次点后 没有连续的点
点赞 回复 分享
发布于 2016-03-29 13:40
360笔试可以用python写吗
点赞 回复 分享
发布于 2016-06-18 11:54
#include <iostream> using namespace std; void f(string &s) { int n = 0; for(auto i = s.begin(); i != s.end() - 1; ++i) { if(*i == '.' && *(i + 1) == '.') ++n; } cout << n << endl; } int main() { int n , m ; int x; char c; while(cin >> n >> m) { string s; for(int i = 0; i < n; ++i) { cin >> c; s.push_back(c); } for(int i = 0; i < m; ++i) { cin >> x >> c; s[x - 1] = c; f(s); } } return 0; }
点赞 回复 分享
发布于 2016-03-29 22:45
#include <iostream> #include <string> using namespace std; int f(string s){ int res = 0, i, j; for(i = 0; i < s.size(); i++){ if(s[i] == '.'){ j = i; while(s[j] == '.'){ j++; } res += j - i - 1; i = j; } } return res; } int main(){ int length, times, position, i; char replace_ch; string s; while(cin >> length >> times >> s){ for(i = 0; i < times; i++){ cin >> position >> replace_ch; s[position - 1] = replace_ch; cout << f(s) << endl;; } } }
点赞 回复 分享
发布于 2016-03-29 21:55
前端怎么是java?不是考js么
点赞 回复 分享
发布于 2016-03-29 08:37
这个题目不看例子,根本就不知道是个什么鬼啊。需要注意同一组数据中每次的字符替换都建立在上一次的基础上。 import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int m = in.nextInt(); StringBuilder sb = new StringBuilder(in.next()); for (int i = 0; i < m; i++) { int count = 0; int x = in.nextInt(); char c = in.next().charAt(0); sb.setCharAt(x-1, c); StringBuilder cp = new StringBuilder(sb.toString()); int length = cp.length(); int j = 0; while (j < length - 1) { if (cp.charAt(j) == '.' && cp.charAt(j+1) == '.') { cp.replace(j, j+2, "."); count++; length = cp.length(); } else { j++; } } System.out.println(count); } } } }
点赞 回复 分享
发布于 2016-03-29 01:39
#include <iostream> #include <cstdio> using namespace std; char ch[300010]; int n,m; int Cal(int x,char c) { x-=1; if(c == '.') //点 { if(ch[x]=='.') {//点换点 ch[x] = c; return 0; } else {//点换字母 int rt = 0; if(x-1>=0 && ch[x-1]=='.') rt += 1; if(x+1<n && ch[x+1]=='.') rt += 1; ch[x] = c; return rt; } } else if(c != '.') {//字母 if(ch[x]!='.') {//字母换字母无意义 ch[x] = c; return 0; } else if(ch[x]=='.') {//字母换点 左边有点少1 右边有点再少1 int rt = 0; if(x-1>=0 && ch[x-1]=='.') rt -= 1; if(x+1<n && ch[x+1]=='.') rt -=1; ch[x] = c; return rt; } } } int main() { while(cin >>n >> m) { getchar(); int count = 0; gets(ch); for(int i=0;i<n;i++) { if(ch[i]=='.'&&ch[i+1]=='.') { count +=1; } } //cout << count << endl; int x; char ct; for(int i=0;i<m;i++) { scanf("%d %c",&x,&ct); //printf("%d %c\n",x,ct); count += Cal(x,ct); cout << count << endl; } } } 就是给你一个字符串,问你替换其中某个字符后,需要几次修改才没有连续的点点...
点赞 回复 分享
发布于 2016-03-28 22:14
import java.util.Scanner; public class Main { public static int f(StringBuilder str, int len) { int cnt = 0, ans = 0; int i=0,j=0; while (j < len) { if(str.charAt(i) == '.') for(j = i+1;j < len; j++) if(str.charAt(j) != '.') break; if(j == len && str.charAt(j-1)=='.') cnt += len - 1 - i; else   cnt += j-1-i; i = j+1; } return cnt; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringBuilder str; int n,m,x,i; char c = '.'; while (scan.hasNext()) { n = scan.nextInt(); m = scan.nextInt(); System.out.println("n: " +n + "m: " + m); str = new StringBuilder(scan.next()); System.out.println(str); for(i = 0; i < n; i++) { x = scan.nextInt(); c = scan.next().charAt(0); str.replace(x-1,x,c+""); System.out.println( str + " " +f(str,n)); } } } }
点赞 回复 分享
发布于 2016-03-28 21:52
import java.util.Scanner; public class Main { public static int f(StringBuilder str, int len) { int cnt = 0, ans = 0; boolean firstDot = true; int i=0,j=0; while (j < len) { if(str.charAt(i) == '.') for(j = i+1;j < len; j++) if(str.charAt(j) != '.') break; if(j == len && str.charAt(j-1)=='.') cnt += len - 1 - i; else cnt += j-1-i; i = j+1; } return cnt; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringBuilder str; int n,m,x,i; char c = '.'; while (scan.hasNext()) { n = scan.nextInt(); m = scan.nextInt(); System.out.println("n: " +n + "m: " + m); str = new StringBuilder(scan.next()); System.out.println(str); for(i = 0; i < n; i++) { x = scan.nextInt(); c = scan.next().charAt(0); str.replace(x-1,x,c+""); System.out.println( str + " " +f(str,n)); } } } }
点赞 回复 分享
发布于 2016-03-28 21:48
一看题目立刻就想到了线段树,有其他想法嘛?
点赞 回复 分享
发布于 2016-03-28 21:33
表示两道题都没有看懂什么意思!!!!
点赞 回复 分享
发布于 2016-03-28 21:22
哈哈哈,我后台的和你前端的额一样,但是我写出来但是调试有问题  #include <stdio.h> int search(char *cpSource) { int iCount=0; while(*(cpSource+index)) { if(*cpSource =="\n") { return 0; } if(*cpSource == ".") { iCount++; } else{ if (iCount<2){ return 0; } else{ return iCount+ search(cpSource)-1; } } ++cpSource; } int main() { int a, b; char s[30000]; char restr; int count = 1; while(scanf("%d%d", &a,&b) != EOF){ for(i=0;i<a;i++) { gets(s);} for(int k=0;k<b;k++){ scanf("%d %s", &index, &restr); s[d+1]=restr; printf("%d", search(s)); return 0; } }
点赞 回复 分享
发布于 2016-03-28 21:22
我感觉是:先进行字符替换,然后计算连续两个点出现的次数。输出次数 我也是把例子完全按照题意推倒了一遍才看懂的
点赞 回复 分享
发布于 2016-03-28 21:19

相关推荐

头像
昨天 20:13
中南大学 Java
序言大家好呀。我是希晨er,一个初入职场的程序猿小登最近上班摸鱼刷到了一篇文章:10年深漂,放弃高薪,回长沙一年有感,还有聊聊30岁大龄程序员过往的心路历程,突然就有点感慨。我如今也做出了和大明哥一样的抉择,只是更早。此外我22年的人生,好像从来没好好记录过。正好现在工作不太忙,就想把这些经历写下来,也希望能得到社区里各位前辈的指点个人背景我是03年出生的西安娃,父母都是普通打工人。刚从中南大学软件工程专业毕业半年,现在在老家的央企过着躺平摆烂的日子成长轨迹从农村到城市的童年我家并不是西安的,只是爸妈在西安上班,从小学之后就把我接到了西安。后来老家房子拆了,爷爷奶奶也搬了过来。农村的生活,我觉...
Yki_:看哭了,恋爱那一段你女朋友说你不够关心她,可你毕竟也愿意遇到矛盾写几千字来和她慢慢分析;说不愿意给她花钱,我感觉可能只是消费观不一样;如果她想留在长沙,也应该提前跟你说开。不过她也许会心疼你放弃大厂offer转向数字马力?我也因为同样的原因有过一段幸福而充满遗憾的感情,不过跟爱情相比确实前途更重要一点。至于offer的选择,换我我也会这么选。把这些旧事记录下来以后,接下来就好好向前看吧,加油兄弟
🍊晨光随笔
点赞 评论 收藏
分享
脾气小祖宗:这简历摸到都得狠狠地消毒液洗手😂
点赞 评论 收藏
分享
09-23 08:41
已编辑
门头沟学院 Java
牛客吹哨人:可恶!它越来越嚣张了...哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务