题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理。先来说说题意吧:

总共有n个人,n个人都会有一段话,先是princess说话,里面如果提到了a1,a2,a3...这几个不同的人的话,对应提到的次数是x1,x2,x3..的话,那么下一个对话是ai这个人说的概率是xi/(x1+x2+x3)....,然后下一个人的对话里也会提到别的人,然后也有一定的概率会有下一轮对话,现在要问的就是,给定了这些对话,问你期望的对话次数是多少。

我们可以设第i个人持续的对话的期望是xi,那么xi应该等于  xi=p1*x1+p2*x2+p3*x3+...+1 对应的pi即为在该人对话出现的次数所对应的频率。然后对每个人列出这样的方程就会构成一系列的方程组。然后高斯消元即可。

难度在于算在某个对话里出现了多少次,因为像如何你用KMP去做匹配 prince是会出现在princess那里的,题目说了人名之间是用五种间隔符隔开的,所以匹配的时候或者匹配前将对话里的每一个词分出来,然后和对应的字符串判相等即可。我写的不好跪了好几发呀- -0

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define maxn 220
#define eps 1e-7
using namespace std; char str[120][1050];
char name[120][20];
int n; double mat[120][120];
char word[1200]; int dcmp(double x) {
return (x > eps) - (x < -eps);
} int cnt(char*T, char *S)
{
int res = 0; int idx = 0;
for (int i = 0; T[i]; i++){
if (!(T[i] <= 'z'&&T[i] >= 'a')) continue;
int id = 0;
while (T[i] <= 'z'&&T[i] >= 'a'){
word[id++] = T[i++];
}
word[id] = '\0';
if (strcmp(word, S) == 0) res++;
i--;
}
return res;
} bool gauss(double mat[120][120], int n)
{
for (int i = 0; i < n; i++){
int pivot = i;
for (int j = i; j < n; j++){
if (abs(mat[j][i])>abs(mat[pivot][i])) pivot = j;
}
swap(mat[i], mat[pivot]);
if (abs(mat[i][i]) < eps) return false;
for (int j = i + 1; j <= n; j++) mat[i][j] /= mat[i][i];
for (int j = 0; j < n; j++){
if (i != j)
for (int k = i + 1; k <= n; k++){
mat[j][k] -= mat[j][i] * mat[i][k];
}
}
}
return true;
} int main()
{
while (cin >> n)
{
getchar();
memset(name, 0, sizeof(name));
memset(str, 0, sizeof(str));
for (int i = 0; i < n; i++){
gets(str[i]);
int j;
for (j = 0; str[i][j]; j++){
if (str[i][j] == ':') break;
name[i][j] = str[i][j];
}
name[i][j + 1] = '\0'; int k = 0; j++;
for (; str[i][j]; j++,k++){
str[i][k] = str[i][j];
}
str[i][k] = '\0';
}
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++){
double tot = 0; double tmp = 0;
for (int j = 0; j < n; j++){
if (i == j) continue;
tmp = cnt(str[i], name[j]);
mat[i][j] = tmp;
tot += tmp;
}
mat[i][i] = -tot; mat[i][n] = -tot;
if (dcmp(tot) == 0){
mat[i][i] = mat[i][n] = -1;
}
}
if (gauss(mat, n)){
printf("%.3lf\n", mat[0][n]);
}
else puts("Infinity");
}
return 0;
}

ZOJ3560 Re:the Princess(高斯消元法)的更多相关文章

  1. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  2. HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站

    别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)     ...

  3. hdu 1026(Ignatius and the Princess I)BFS

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. hdu1026.Ignatius and the Princess I(bfs + 优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  7. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. POJ 1222 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...

  9. ACM: HDU 1028 Ignatius and the Princess III-DP

     HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Form ...

随机推荐

  1. 【WPF学习日记】——Window的DataContext绑定ViewModel

    1.全局的ViewModel绑定: a)设定全局的ViewModel(App.xaml中): 1 <Application x:Class="MyTest.App" 2 xm ...

  2. ED/EP系列3《基本指令》

    Ø --APPLICATIONBLOCK(应用锁定): Ø --APPLICATION UNBLOCK(应用解锁): Ø --CARDBLOCK(卡片锁定): Ø --EXTERNAL AUTHENT ...

  3. 通过java反射实现简单的关于MongoDB的对象关系映射(ORM).

    通过阅读MongoDB  3.2.1的官方文档中关于java 编程发现最新的文档并没有实现对对象到Document的映射,所以自己有了利用反射实现简单的关系映射. 1.定义抽象类:AbstractMo ...

  4. Unity3d Shortcuts

    参考:http://www.ceeger.com/Manual/ 场景视图导航  Click-drag to drag the camera around. 点击拖拽平移场景视图 Hold Alt a ...

  5. wifi-sdio接口

    1.sdio接口层解析 SDIO总线 SDIO总线和USB总线类似,SDIO也有两端,其中一端是HOST端,另一端是device端.所有的通信都是由HOST端发送命令开始的,Device端只要能解析命 ...

  6. ASP.NET MVC掉过的坑_MVC初识及MVC应用程序结构

    APS.Net MVC 浅谈[转] 来自MSDN 点击访问 MVC 理论结构 模型-视图-控制器 (MVC) 体系结构模式将应用程序分成三个主要组件:模型.视图和控制器. ASP.NET MVC 框架 ...

  7. Learning note for Binding and validation

    Summary of my learning note for WPF Binding Binding to DataSet. when we want to add new record, we s ...

  8. c++实例化对象

    今天看到c++实例化对象,有点懵了.Activity_Log the_log (theLogPtr, Tree->GetBranch());这是那一段小代码,开始没看懂.java看习惯了总喜欢n ...

  9. BICEP单元测试计划-四则运算-测试

    一.6个值得测试的具体部位,他们能够提高你的测试技巧 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制 ...

  10. Careercup - Microsoft面试题 - 6337018766295040

    2014-05-10 06:38 题目链接 原题: What do you think is the next big thing in technology? For example, search ...