在庆祝祖国母亲70华诞之际,老师给小乐乐出了一个问题。大家都知道China的英文缩写是CHN,那么给你一个字符串s,你需要做的是统计s中子序列“CHN”的个数。
子序列的定义:存在任意下标a < b < c,那么“s[a]s[b]s[c]”就构成s的一个子序列。如“ABC”的子序列有“A”、“B”、“C”、“AB”、“AC”、“BC”、“ABC”。
子序列的定义:存在任意下标a < b < c,那么“s[a]s[b]s[c]”就构成s的一个子序列。如“ABC”的子序列有“A”、“B”、“C”、“AB”、“AC”、“BC”、“ABC”。
输入只包含大写字母的字符串s。(1 ≤ length ≤ 8000)
输出一个整数,为字符串s中子序列“CHN”的数量。
CCHNCHN
7
CCHNCHNCHNCHN
30
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; long int count = 0; long int c = 0, h = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'C') ++c; else if (s[i] == 'H') h += c; else if (s[i] == 'N') count += h; } cout << count << endl; return 0; }
import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int i = 0; int j = 0; int k = 0; char[] ss = s.toCharArray(); int len = s.length(); int to=0; for (;i<len;i++){ for (j=i+1;j<len;j++){ for (k=j+1;k<len;k++){ if(ss[i]=='C'&&ss[j]=='H'&&ss[k]=='N') to ++; } } } System.out.println(to); } }
/*统计字符串CCHNCHN 的子串CHN的个数*/ #include<cstdio> (802)#include<cstring> const int maxn = 100010; const int mod = 1000000007; char str[maxn]; //字符串 int leftNumC[maxn] = {0}; //每一位左边含有p的个数 int main() { gets(str); int len = strlen(str); for(int i = 0; i<len; i++) { if(i>0) { leftNumC[i] = leftNumC[i-1]; } if(str[i] == 'C') { leftNumC[i]++; //当前为是C,则C的个数增加1 } } int ans = 0, rightNumN = 0; //ans为答案,rightNumT记录右边N的个数 for(int i = len-1; i>=0; i--) { if(str[i] == 'N') { rightNumN++; }else if(str[i] == 'H') { ans = (ans + leftNumC[i] * rightNumN)%mod; } } printf("%d\n",ans); return 0; }
#include <stdio.h> (737)#include <string.h> int main() { char str[8005]; gets(str); long long i,cnt_c=0,cnt_h=0,cnt_n=0; for(i=0;str[i]!='\0';i++){ if(str[i]=='C') cnt_c++; else if(str[i]=='H') cnt_h+=cnt_c; else if(str[i]=='N') cnt_n+=cnt_h; //printf("%c",str[i]); } printf("%lld\n",cnt_n); }
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> int main() { char a[8000]; gets(a); long int c = 0, h = 0, n = 0; for (int i = 0; i <= strlen(a); i++) { if (a[i] == 'C') { c++; } else if (a[i] == 'H') { h += c; } else if (a[i] == 'N') { n += h; } } printf("%ld", n); return 0; }
#include<stdio.h> #include<string.h> int main(void) { char ch[8001] = { 0 }; gets(ch); int i = 0; int j = 0; int k = 0; int len = strlen(ch); unsigned long int sum1 = 0; unsigned long int sum2 = 0; int flag1 = 10000; int flag2 = 10000; int ret = 0; for (i = 0; i < len; ++i) { if (ch[i] == 'C') { sum2 = 0; for (j = i + 1; j < len; ++j) { if (ch[j] == 'H') { ret = 0; for (k = j + 1; k < len; ++k) { if (ch[k] == 'N') { ++ret; } } ++j; sum2 += ret; while (ch[j] == 'H') { sum2 += ret; ++j; } } } sum1 += sum2; ++i; while (ch[i] == 'C') { sum1 += sum2; ++i; } } } printf("%u\n", sum1); return 0; }
#include<stdio.h> #include<string.h> int main(){ char str[9000]; scanf("%s\n",str); char* p=str; long long count1=0; long long count2=0; long long count3=0; while(*p){ if(*p=='C') count1++; else if(*p=='H') count2+=count1; else if(*p=='N') count3+=count2; p++; } printf("%lld\n",count3); return 0; }这个代码是不是有问题呀,如果最后一个字符输出的不是N,输出的结果和预想的结果是不一样的吧。
#include <stdio.h> int main(){ char s[8001]; scanf("%s",s); int chns[8000][2],chnsIndex=0,chnsLength; int tempCount=0; char *sp = s; while(*sp != '\0'){ if(*sp =='C'){ tempCount++; }else if (*sp =='H'){ chns[chnsIndex++][0]=tempCount; } sp++; } tempCount=0; chnsLength=chnsIndex; chnsIndex--; while(sp != s){ if(*sp =='N'){ tempCount++; }else if (*sp =='H'){ chns[chnsIndex--][1]=tempCount; } sp--; } int i ; long total=0; for (i=0;i<chnsLength;i++){ total+= chns[i][0] *chns[i][1]; } printf("%ld\n",total); return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); long C=0,CH=0,CHN=0; for(int i = 0;i<s.length();i++) { if(s.charAt(i) == 'C') C++; else if(s.charAt(i) == 'H') CH+=C; else if(s.charAt(i) == 'N') CHN+=CH; } System.out.println(CHN); } }