k questions:
Meaning of the questions:
Give you a bunch of strings consisting of numbers, you find a substring from a string in the string is a multiple of 300
answer:
B title and third this question is very similar
First of all can be a multiple of separate three hundred, and if necessary a multiple of 100 and 3
Requires multiple is 100 must be followed by two 0
3 can be a multiple of the digital substring is a multiple of 3 and to determine
Then the substring violence to calculate sure time out, so this is where you want to optimize multiple of 3
First, we want to initialize the string, and the results of its prefix after taking more than 3
First, the emergence of a special judge to 0, because to say on the subject can be considered in multiples of 300 0
Secondly, I think about as the result of the position taken if the string is two more than 3 (assuming that the results of 2), then the middle section of the two positions is a multiple of 3
Because we pretreatment is the result of a prefix and a modulo 3, then take the remainder if the result of 3 prior to the first position 2, and there is also a position to take more than 2, then let it reduce a position after the prefix prefix to a position before, so this is not put to eliminate 2 yet, it is possible to reduce this complexity by
There are top just said is a multiple of three, did not say that is a multiple of 300, but also to determine the position and therefore the position of the front is not 0
On the code:
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1e5+10; 7 char str[maxn]; 8 int w[maxn],v[maxn]; 9 int main() 10 { 11 scanf("%s",str+1); 12 int len=strlen(str+1); 13 int ans=0; 14 for(int i=1;i<=len;++i) 15 { 16 ans=ans+str[i]-'0'; 17 ans%=3; 18 v[i]=ans; 19 } 20 long long sum=0; 21 for(int i=1;i<=len;++i) 22 { 23 if(str[i]=='0') 24 { 25 SUM ++ ; 26 is IF (STR [I- . 1 ] == ' 0 ' ) 27 { 28 SUM = + W [V [I]]; 29 } 30 } 31 is W [V [I- . 1 ]] ++; // this is to limit the number of sub-strings is greater than 1, // because a 0 has been sentenced Unexamined 32 } 33 is the printf ( " % LLD \ n- " , SUM); 34 is return 0; 35 }
j title:
This question is the shortest path problem encountered layered graph, I originally wanted to record through the shortest path, and then removed at the edge of the largest on the shortest route, but has been RE, had to come up with a template layered shortest
Stratified shortest explain ----> Portal
I am here to put him to take over the template ^ _ ^
The first (add)
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int INF=0xffffff; 8 const int maxn=1e7+10; 9 int n,m,nnn[maxn],fir[maxn],to[maxn],val[maxn],cnt,dis[maxn],d[maxn],w[maxn],q[maxn]; 10 //int flag[maxn][maxn]; 11 struct shudui1 12 { 13 int start,value; 14 bool operator <(const shudui1 q)const 15 { 16 return value>q.value; 17 } 18 } str1; 19 priority_queue<shudui1>r; 20 void JK() 21 { 22 memset(dis,0,sizeof(dis)); 23 while(!r.empty()) 24 { 25 str1=r.top(); 26 r.pop(); 27 int x=str1.start; 28 if(dis[x]) continue; 29 dis[x]=1; 30 for(int i=fir[x]; i!=-1; i=nnn[i]) 31 { 32 if(!dis[to[i]] && d[to[i]]>d[x]+val[i]) 33 { 34 str1.value=d[to[i]]=d[x]+val[i]; 35 str1.start=to[i]; 36 r.push(str1); 37 } 38 } 39 } 40 } 41 void init() 42 { 43 memset(d,0x3f,sizeof(d)); 44 memset(fir,-1,sizeof(fir)); 45 cnt=0; 46 } 47 void add_edge(int x,int y,int z) 48 { 49 nnn[++cnt]=fir[x]; 50 fir[x]=cnt; 51 to[cnt]=y; 52 val[cnt]=z; 53 } 54 int main() 55 { 56 int s,t,k; 57 //memset(path,-1,sizeof(path)); 58 scanf("%d%d%d%d%d",&n,&m,&s,&t,&k); 59 init(); 60 while(m--) 61 62 { 63 64 int u, v, w; 65 66 scanf("%d%d%d",&u, &v, &w); 67 68 for(int i = 0; i <= k; i++) 69 70 { 71 72 add_edge(u + i * n, v + i * n, w); 73 74 add_edge(v + i * n, u + i * n, w); 75 76 if(i != k) 77 78 { 79 80 add_edge(u + i * n, v + (i + 1) * n, 0); 81 82 add_edge(v + i * n, u + (i + 1) * n, 0); 83 84 } 85 86 } 87 88 } 89 str1.start=s; 90 d[s]=0; 91 str1.value=0; 92 r.push(str1); 93 JK(); 94 int ans=INF; 95 for(int i = 0; i <= k; i++) 96 ans = min(ans, d[t + i * n]); 97 printf("%d\n",ans); 98 return 0; 99 }
The second (increase in dimension)
1 #include <iostream> 2 3 #include <string.h> 4 5 #include <stdio.h> 6 7 #include <algorithm> 8 9 #include <queue> 10 11 #include <vector> 12 13 #define ll long long 14 15 #define inf 0x3f3f3f3f 16 17 #define pii pair<int, int> 18 19 const int mod = 1e9+7; 20 21 const int maxn = 1e5+7; 22 23 using namespace std; 24 25 struct node{int to, w, next, cost; } edge[maxn]; 26 27 int head[maxn], cnt; 28 29 int dis[maxn][15], vis[maxn][15]; 30 31 int n, m, s, t, k; 32 33 struct Dijkstra 34 35 { 36 37 void init() 38 39 { 40 41 memset(head,-1,sizeof(head)); 42 43 memset(dis,127,sizeof(dis)); 44 45 memset(vis,0,sizeof(vis)); 46 47 cnt = 0; 48 49 } 50 51 52 53 void add(int u,int v,int w) 54 55 { 56 57 edge[cnt].to = v; 58 59 edge[cnt].w = w; 60 61 edge[cnt].next = head[u]; 62 63 head[u] = cnt ++; 64 65 } 66 67 68 69 void dijkstra() 70 71 { 72 73 priority_queue <pii, vector<pii>, greater<pii> > q; 74 75 dis[s][0] = 0; 76 77 q.push({0, s}); 78 79 while(!q.empty()) 80 81 { 82 83 int now = q.top().second; q.pop(); 84 85 int c = now / n; now %= n; 86 87 if(vis[now][c]) continue; 88 89 vis[now][c] = 1; 90 91 for(int i = head[now]; i != -1; i = edge[i].next) 92 93 { 94 95 int v = edge[i].to; 96 97 if(!vis[v][c] && dis[v][c] > dis[now][c] + edge[i].w) 98 99 { 100 101 dis[v][c] = dis[now][c] + edge[i].w; 102 103 q.push({dis[v][c], v + c * n}); 104 105 } 106 107 } 108 109 if(c < k) 110 111 { 112 113 for(int i = head[now]; i != -1; i = edge[i].next) 114 115 { 116 117 int v = edge[i].to; 118 119 if(!vis[v][c+1] && dis[v][c+1] > dis[now][c]) 120 121 { 122 123 dis[v][c+1] = dis[now][c]; 124 125 q.push({dis[v][c+1], v + (c + 1) * n}); 126 127 } 128 129 } 130 131 } 132 133 } 134 135 } 136 137 }dj; 138 139 140 141 int main() 142 143 { 144 145 while(~scanf("%d%d%d%d%d", &n, &m,&s,&t &k)) 146 147 { 148 149 dj.init(); //scanf("%d%d",&s,&t); 150 151 while(m--) 152 153 { 154 155 int u, v, w; 156 157 scanf("%d%d%d",&u, &v, &w); 158 159 dj.add(u, v, w); 160 161 dj.add(v, u, w); 162 163 } 164 165 dj.dijkstra(); 166 167 int ans = inf; 168 169 for(int i = 0; i <= k; i++) 170 171 ans = min(ans, dis[t][i]); 172 173 printf("%d\n", years); 174 175 } 176 177 }