当前位置:网站首页>Score of sub series of previous test questions and [11th] [provincial competition] [group B]
Score of sub series of previous test questions and [11th] [provincial competition] [group B]
2022-06-10 00:39:00 【6 Yingchen】
Substring score and full decomposition method
List of articles
One 、 Complete title
Two 、 Specific ideas
1. Recurrence of violence
Using the idea of recursion , Find each string , And count the score of each string , Finally, carry out statistical accumulation , Get the result , The order of the string is :
- First , A complete string , Remove the last character of a complete string at a time
- Again , Remove the first character of the string , repeat 1 The operation of
- repeat 2 Until the string is empty
After finding each substring , Just count the number of characters
2. Statistical contribution value
Normally, the string should be viewed horizontally line by line , Let's open our minds , Look up , Look how many times the characters appear in each column , Count the times , I found a schematic diagram on the Internet :
source :https://img-blog.csdnimg.cn/20210413181240678.png
We can sum up the formula :
- When the character first appears : Number of occurrences =( Subscript +1)x( String length - Subscript )
- An obsolete character appears before this character : Number of occurrences =( String length - Subscript )x( The current position - Where it happened before )
3、 ... and 、 Write code
1. Recurrence of violence
#include <stdio.h>
#include <string.h>
#define MAX 100000// Maximum length
int difStr(char s[],int len)// Find the score of the string
{
if(len==0) return 0;// The end condition
int i,cnt=0;
int a[26]={
0};// Whether statistics exist
for(i=0;i<len;i++) a[s[i]-'a']++;// Count
for(i=0;i<25;i++) if(a[i]!=0) cnt++;// Count the number of different letters
cnt+=difStr(s,len-1);// Recursive implementation
return cnt;// Returns the score of this string
}
int f(char s[],int len)// Count the scores and of all strings
{
if(len==0) return 0;// The end condition
int sum=0;// Score sum
sum+=difStr(s,len);// Find the string score of this round of recursion and
sum+=f(s+1,len-1);// Recursively find the sum of other strings
return sum;// Returns the score and of all strings
}
int main()
{
char s[MAX];// Open array
int sum,len;
scanf("%s",&s);// Input string
len=strlen(s);// Find the length of the input string
printf("%d",f(s,len));// Print the results
return 0;
}
2. Statistical contribution value
#include<stdio.h>
#include<string.h>
#define MAX 100000// Maximum length
typedef long long int LLint;
int main()
{
LLint i,len,sum=0;
int a[26]={
0}; // Used to count the number of characters
char s[MAX];
scanf("%s",s);// Input string
len=strlen(s);// Find the length of the input string
// Find the sum of string scores
for(i=0;i<len;i++)
{
if(a[s[i]-'a']==0)// Judge whether the character has appeared
{
sum=sum+(i+1)*(len-i);// Add up the values of individual characters
a[s[i]-'a']=i+1;// Where to save characters
}
else
{
sum=sum+(i+1-a[s[i]-'a'])*(len-i);// Characters that have appeared before , Now the value that appears again
a[s[i]-'a']=i+1;// Update location
}
}
printf("%lld",sum);// Printout
return 0;
}
Four 、 Evaluation results
1. Recurrence of violence
It's just past 50% Test point of , Later test points will time out
2. Statistical contribution value
5、 ... and 、 Summarize and evaluate
In conclusion, we can see that , Although the idea of recursive method is clear , Write simple , But it's more violent, and the time complexity is higher ; And the method of statistical contribution value , It's not easy to think of , But it's very simple .
therefore , If we live in a picture , Always jump out , Take a look at the picture , Jumping back , Instead of working hard , Sometimes choice is more important than effort .
If you have any questions, you are welcome to point out
The Blue Bridge Cup series will continue to be updated , Welcome to your attention , Learning together
边栏推荐
- flutter pub get failed (66; Could not find a file named “pubspec.yaml“
- 可重复读隔离级别的基石MVCC机制
- 低边驱动和高边驱动
- MySQL execution plan
- Idea uninstall tutorial
- 力扣 无重复字符的最长子串 C语言 题解
- Disorder of flinksql
- 2022最新版阿里开发手册发布!!!
- How to keep the contents of WPS merged cells in one cell
- Can the merger of G7 and E6 of head Internet of things SaaS become a "meituan" in the field of to B?
猜你喜欢

Mat数据的深浅拷贝

Operator (day 2)

Sentinel-3 data introduction

Mind map - 3. SQL injection vulnerability

The problem of connecting the computer to the printer (the printer display is not specified) solution

力扣 无重复字符的最长子串 C语言 题解

ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks

if判斷是否為空時的函數選擇

尽一份孝心,为家人做一个老人防摔报警系统

SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统
随机推荐
二叉树展开为链表[树处理的精髓--如何处理每一颗子树]
1049大盗阿福
How WPS merges cells with different sizes
谁说Redis不能存大key
力扣 自除数 C语言 题解
What does in place operation mean in programming?
[docker]mysql scheduled backup
JVM explanation
试题 历届真题 完全二叉树的权值【第十届】【省赛】【B组】
应用最新的AD和TXK补丁
Who says redis can't save big keys
The essence and soul of message queue
The problem of connecting the computer to the printer (the printer display is not specified) solution
期货开户网上安全性可以保证吗?哪家期货公司好?
Go profile management -viper
View the installable version number of the wheel on this computer
run npm fund for details
2022最新版阿里开发手册发布!!!
IDC authority predicts that China's manufacturing industry will soon take advantage of the cloud
Where is the precise position of PS ruler adjustment