有参加今晚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
我感觉是:先进行字符替换,然后计算连续两个点出现的次数。输出次数 我也是把例子完全按照题意推倒了一遍才看懂的
点赞 回复 分享
发布于 2016-03-28 21:19
哈哈哈,我后台的和你前端的额一样,但是我写出来但是调试有问题  #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:22
一看题目立刻就想到了线段树,有其他想法嘛?
点赞 回复 分享
发布于 2016-03-28 21:33
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
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
#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.*; 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
前端怎么是java?不是考js么
点赞 回复 分享
发布于 2016-03-29 08:37
#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
#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
360笔试可以用python写吗
点赞 回复 分享
发布于 2016-06-18 11:54

相关推荐

评论
点赞
5
分享

创作者周榜

更多
牛客网
牛客企业服务